katana90489 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monk community My name is mark and this is my first post and i am in need of some help, i have been trying to teach my self perl programming and i am really stuck on how to do a few things. I want to create a program that will generate a number between one and six and repeat until the number 6 has been displayed, i also want to display how many time it took the program to get to the number six Many thanks Mark

Replies are listed 'Best First'.
Re: Perl script newbie need help !!!
by CountZero (Bishop) on Nov 11, 2010 at 18:16 UTC
    Mark,

    Welcome to our Monastery!

    A golden rule to remember is that we help those best, who help themselves.

    Showing what you tried yourself, even when it failed, is always considered a good sign and the Monks will rush to your aid. Just asking for some code, is frowned upon as we are no code writing or homework solving establishment.

    So, show the Monks some of your code and we will speak again!

    A hint: have a look at the rand function.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Perl script newbie need help !!!
by Marshall (Canon) on Nov 11, 2010 at 19:17 UTC
    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.
      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
Re: Perl script newbie need help !!!
by jwkrahn (Abbot) on Nov 11, 2010 at 18:28 UTC

    The program I think you are asking for:

    #!/usr/bin/perl use warnings; use strict; use constant UPPER_LIMIT => 6; my ( @numbers, $count ); while ( @numbers < UPPER_LIMIT ) { my $guess = int 1 + rand UPPER_LIMIT; push @numbers, $guess unless grep $_ == $guess, @numbers; ++$count; } print map( "$_\n", @numbers ), "How many times to get to ", UPPER_LIMIT, " is $count\n"; __END__

    How the program should really be written:

    #!/usr/bin/perl use warnings; use strict; use List::Util qw/ shuffle /; use constant UPPER_LIMIT => 6; my @numbers = shuffle 1 .. UPPER_LIMIT; print map( "$_\n", @numbers ), "How many times to get to ", UPPER_LIMIT, " always ONE\n"; __END__
      Should be written? I've never seen a die like that :)
      Update: I understand the spec this way:
      while ($n = int (rand 6) ){ print 6-$n,"\n"; $count++; } print "6\nTried ",1+$count," times.\n";