Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Send x% of the users to A, the rest to B

by Perleone (Initiate)
on Oct 27, 2012 at 20:04 UTC ( #1001227=note: print w/ replies, xml ) Need Help??


in reply to Re: Send x% of the users to A, the rest to B
in thread Send x% of the users to A, the rest to B

Cool snippet! Interesting to see that (at least on my Debian box) it takes a few dozen iterations to actually reach middle ground:

perl -Mstrict -Mwarnings -E 'my $WIDTH = 50; my ( $try, $i, $j ) = (0) x 3; for ( 1 .. 40 ) { ++$try; my $rand = `perl -E "say rand()"`; $rand < .5 ? ++$i : ++$j; printf qq[%${WIDTH}s : %s\n], "#" x ( $i * $WIDTH / $try ), "#" x ( $j * $WIDTH / $try ); }'


Comment on Re^2: Send x% of the users to A, the rest to B
Download Code
Re^3: Send x% of the users to A, the rest to B
by BrowserUk (Pope) on Oct 27, 2012 at 21:02 UTC
    Interesting to see that ... it takes a few dozen iterations to actually reach middle ground:

    That is the nature of randomness. If it was exactly 50:50, it wouldn't be random.

    But the more trials you run, the closer it will tend. This is more evident if you run this version with various different resets:

    C:\test>perl -E"{++$t;$r=`perl -E\"say rand()\"`;$r<.5?++$a:++$b; printf qq[\r%.3f +: %.3f], $a*100/$t, $b*100/$t; $t>$ARGV[0] and say and $t=$a=$b=0; re +do}" 10 45.455 : 54.545 27.273 : 72.727 36.364 : 63.636 54.545 : 45.455 72.727 : 27.273 63.636 : 36.364 36.364 : 63.636 45.455 : 54.545 27.273 : 72.727 36.364 : 63.636 72.727 : 27.273 45.455 : 54.545 54.545 : 45.455 63.636 : 36.364 C:\test>perl -E"{++$t;$r=`perl -E\"say rand()\"`;$r<.5?++$a:++$b; printf qq[\r%.3f +: %.3f], $a*100/$t, $b*100/$t; $t>$ARGV[0] and say and $t=$a=$b=0; re +do}" 100 52.475 : 47.525 52.475 : 47.525 52.475 : 47.525 46.535 : 53.465 45.545 : 54.455 51.485 : 48.515 41.584 : 58.416 46.535 : 53.465 42.574 : 57.426 40.594 : 59.406 C:\test>perl -E"{++$t;$r=`perl -E\"say rand()\"`;$r<.5?++$a:++$b; printf qq[\r%.3f +: %.3f], $a*100/$t, $b*100/$t; $t>$ARGV[0] and say and $t=$a=$b=0; re +do}" 1000 49.750 : 50.250 51.349 : 48.651 48.651 : 51.349 53.047 : 46.953

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-11-23 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls