http://www.perlmonks.org?node_id=490533


in reply to Runs and Sequences

Update: longest value.
#!/usr/local/bin/perl use strict; use warnings; my ($h, $t, $last, $lt, $lh) = (0,0,"T",0,0); while () { my $random = int( rand(2)); if ($random == 0) { print "T $t times. Longest $lt\n" if $t; $lt<$t?$lt=$t:(); $t=0; $h++; } if ($random == 1) { print "H $h times. Longest $lh\n" if $h; $lh<$h?$lh=$h:(); $h=0; $t++; } }

Replies are listed 'Best First'.
Re^2: Runs and Sequences
by twofish (Initiate) on Sep 09, 2005 at 12:22 UTC
    uksza ..wow, for the most part exactly what I was looking for, you're fast. Thanks to Roy Johnson also. I think this gives me a couple different ways I can generate and parse through the data. I'll work on customizing it to find and grab some other stats that might be kind of cool (like how many tries somone would have to go through on average to get a run of 16 ..should be around 1/(2^16).
    Thanks a lot!
      I'm no mathematician, but shouldn't it be 2/(2^16) = 1/2^15? I mean you can have a string of either all heads or all tails, right?

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come