Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This approach can make the Fisher-Yates shuffle arbitrarily accurate. It would be possible, but messy, to apply it to sort values that compared equal, too.

I fail to see the difference. Certainly one can make any numerically-limited algorithm "arbitrarily accurate" if one is willing to increase the number of bits used to represent the numbers in the calculation. The variation of Fisher-Yates that you propose would require a special check to handle the highly improbable case that the random number generator produced a number that was exactly equal to k/N, for any integer k in 0..N - 1, in order to generate more bits to break the tie (without this provision, the algorithm is identical to the standard Fisher-Yates as far as the uniformity of the sampling is concerned). Likewise, the tag-sorting shuffle algorithm I posted would need to be modified to handle the highly improbable case that two of the random tags happened to be identical (which would result in a stable-sort artifact), by generating more bits to break the tie.

...but Fisher-Yates wins by being O(N) instead of O(NlogN).

Yes, of course, but the speed superiority of Fisher-Yates has never been in question. My position all along has been limited to defending the algorithm I posted against the claim that it was logically flawed in the same way as the sort-based algorithms discussed in When the Best Solution Isn't are. The problem with those algorithms would remain even if we had infinite-precision computers at our disposal; this is not the case for the sort-based algorithm I posted. Furthermore, in comparison to the errors incurred by those truly flawed algorithms, the errors incurred by numerically-limited algorithms like Fisher-Yates or the one I posted are entirely insignificant.

Update: Fixed minor typo/error above: the range 0..N - 1 mentioned towards the middle of the first paragraph was erroneously given as 1..N - 1 in the original post. Also, immediately before in the same sentence, the reference to the variable k was missing.

the lowliest monk


In reply to Re^7: Functional shuffle by tlm
in thread Functional shuffle by Roy Johnson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-19 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found