So you have actually asked what can be a very complicated question. Here is one link for you: randomness
I made a little program to satisfy your requirement and then added a little histogram and a way to loop multiple times (now 10 times). Program rolls dice until it sees a 6, then stops. Along the way to the 6, it keeps track of other numbers rolled. The int(rand(max+1)) gives out numbers from 0..max.
Only this part is needed for the original question:#!/usr/bin/perl -w use strict; my %histo; for (1..10) { my $count=0; while ( (my $r = int(rand(6)) +1 ) != 6) { #int(rand(6) generates nums 0-5 $count++; $histo{$r}++; } #update #### $count++; # there is an off by one error below print "took $count tries to get a 6\n"; $histo{6}++; } foreach my $side (sort keys %histo) { print "number $side rolled $histo{$side} times\n"; } __END__ took 18 tries to get a 6 took 5 tries to get a 6 took 9 tries to get a 6 took 1 tries to get a 6 took 3 tries to get a 6 took 1 tries to get a 6 took 1 tries to get a 6 took 0 tries to get a 6 took 15 tries to get a 6 took 13 tries to get a 6 number 1 rolled 15 times number 2 rolled 17 times number 3 rolled 11 times number 4 rolled 15 times number 5 rolled 8 times number 6 rolled 10 times
Update: of course there is an off by one error in the loop above, $count doesn't get incremented when loop condition is satisfied, so all try counts should be +1. I took that into account for the histo, but not for the simple count.my $count=0; while ( (my $r = int(rand(6)) +1 ) != 6) { #int(rand(6) generates nums 0-5 $count++; } print "took ++$count tries to get a 6\n";
In reply to Re: Perl script newbie need help !!!
by Marshall
in thread Perl script newbie need help !!!
by katana90489
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |