Hello, Monks. I am very new here, (my first post) and am trying to learn perl. For my first little exercise, I am trying to write a script to print out a series of 6 random lotto numbers. I have loaded an array with numbers 1to 49, and even printed it out to make sure it was loaded correctly. However, when the program runs, it occasionally ends up with a zero as on of my lucky picks, and I have no idea where this is coming from, as it is not one of the 49 choices!
#!/usr/bin/perl # Program to pick 6 random & unique lottery numbers from a list of 49. # # # use strict; use warnings; $loop = 50; # number of picks to make while ($loop) { @choices = (1 .. 49); # list of available numbers # print "@choices \n"; # just for testing $count = 5; # number of picks less 1 @picks = (); # initialize/clear the array $num = rand(@choices); # get 1st random pick push (@picks, $num); # and load it into array while ($count) { # now get 5 more numbers $num = rand(@choices); if ($num ~~ @picks) { # if we already have this number, tr +y again next; } # end if push (@picks, $num); # add new number to list $count -- ; # decrement count and get another nu +mber } # end while $count @picks = sort { $a <=> $b } @picks; printf ("Lucky numbers: %02d %02d %02d %02d %02d %02d \n\n", @picks +); $loop -- ; # decrement counter } # end while $loop exit;

In reply to Where is the zero coming from? by Pauleduc

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.