<?xml version="1.0" encoding="windows-1252"?>
<node id="1005078" title="Re: Is this really random?" created="2012-11-22 03:39:17" updated="2012-11-22 03:39:17">
<type id="11">
note</type>
<author id="27571">
ColonelPanic</author>
<data>
<field name="doctext">
&lt;p&gt;A simple test:&lt;/p&gt;
&lt;code&gt;
my $sum = 0;
for( 1..10_000_000 ) { 
    $sum++ if coin_toss() eq 'heads';
}

print $sum;
&lt;/code&gt;
&lt;p&gt;The result I got was 5,000,938, meaning heads happened 50.01% of the time. Seems pretty reasonable.&lt;/p&gt;
&lt;p&gt;Most people's perceptions of randomness are notoriously faulty. For example, they seriously underestimate the number of times repeated numbers will appear in a sequence.&lt;/p&gt;
&lt;p&gt;One suggestion: I think it is clearer to use int() to specify that you want a random &lt;i&gt;integer&lt;/i&gt;:&lt;/p&gt;
&lt;code&gt;
    my $index = int rand @coin;
    return $coin[ $index ];
&lt;/code&gt;
&lt;p&gt;As your code is written, you are using a floating point number as the array index.  This works (because it is converted to integer), but it is a little unusual.  You wouldn't write &lt;code&gt; $array[1.5] &lt;/code&gt;&lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-27571"&gt;
&lt;br&gt;&lt;br&gt;When's the last time you used duct tape on a duct?  --Larry Wall
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1005043</field>
<field name="parent_node">
1005043</field>
</data>
</node>
