Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

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

Good looking out: obviously not optimal. I tried to figure out for myself what was going wrong.

First, I simplified the code itself a little further:

print join "", sort { rand() <=> 0.5 } split //, "abcde";

That does basically the same thing, except calls rand() once less per comparison.

Staring at it didn't do much good for me so I came up with a little test script of my own:

use strict; use warnings; sub compare { my ($a, $b) = @_; my $rand = rand(); print "$a: $rand; $b: 0.5\n"; return $rand <=> 0.5; } print join "", sort { compare($a, $b) } split //, "abcde";

The output of which was something like:

$ perl test.pl a: 0.876941476789401; b: 0.5 c: 0.0768385185833438; d: 0.5 b: 0.203632482365844; c: 0.5 c: 0.234952540459695; a: 0.5 a: 0.746254431212112; d: 0.5 b: 0.648884088019244; e: 0.5 ebcda

After running this a few times and looking at the results, it hit me. This is going to be a rough explanation, but I'll give it a shot. After comparing the first four letters to one another it selects the "smallest" one (the one with the smallest rand() anyway) and compares it to the final letter: 'e' in this case. Because 'e' is the last to be compared (in the initial go, at least) and because the rand() number is regenerated at each comparison, it always has roughly a 50% chance of being the "smallest" since it has to only pass a single test to be the "smallest". The letters in front of it must not only pass their first test, but will always have to be compared to the letters following it, thus reducing their chances of being the "smallest".

In this way, my "solution" favors the letters towards the end and is more likely to stick them in front. That's why the distribution is so uneven. If you follow sort()'s algorithm ( quicksort, I believe (C's qsort(), I think ) ) through, it'll make sense.

I hope that makes some sense ...

Zenon Zabinski | zdog | zdog@perlmonk.org


In reply to Re^3: Randomize word by zdog
in thread Randomize word by vikee

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 having a coffee break in the Monastery: (6)
As of 2024-03-28 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found