in reply to Perl script newbie need help !!!

I am guessing that you are working some kind of dice game. The "built-in" random number generators in Perl, like in C and other languages aren't really that random. They are fast and good enough for lots and lots of things. There are other algorithms that are slower, but better - better meaning more like true randomness. You will have to do some investigation about this if you are really going to get serious about modeling some betting strategy or whatever.

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.

#!/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
Only this part is needed for the original question:
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";
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.

Replies are listed 'Best First'.
Re^2: Perl script newbie need help !!!
by davies (Monsignor) on Nov 12, 2010 at 00:34 UTC
    took 0 tries to get a 6
    I've got a tournament backgammon match on Monday. If you can tell me how to get some sixes without throwing the dice, I'd be ever so grateful.

    Yours not very sincerely, :-)

    John Davies
      Ha, a off by error! was ok for histo, but not $count. The general idea of how to do it remains the same as well as comments on imperfections in the standard rand() function. the link I provided is a good starting place.

      good luck in your backgammon match!

      all the numbers have already occured. the visual display is just proof that they occured in a particular sequence.
      mathematical true randomness can never be reached, as you would need an infinitely large set (number of dice throws.)
      in real life a dice can actually land on edge, and also any number of factors can introduce a bias. a program/script to generate dice throws or such, is a limited model.
      if this is indeed some kind of dice throwing pretend problem, it should clearly define the limits/assumptions. as the required output for this excercise can change quite a bit depending on these assumptions.
      the hardest line to type correctly is: stty erase ^H