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.

In reply to Re: Perl script newbie need help !!! by Marshall
in thread Perl script newbie need help !!! by katana90489

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.