Jenda Krynicky wrote: > > From: "Mahendra Thacker" <mnt_cal@snet.net> > > > > # Cash5 Lotto: draw of 6 unique non-zero numbers from 1..35 > > > > my $top = 36; # rand gives a rand numb less than top, hence > > my @myarr = (); > > my $myrand; > > > > while ( @myarr < 6 ) { > > $myrand = rand $top; > > $myrand %= 35; > > next if $myrand =~ 0; > > $t = join ' ', @myarr; > > next if $t =~ $myrand; > > push @myarr, $myrand; > > } > > print join ' ', @myarr; > > You have a bug in there. This would never give you 35. Keep in ^^^^^ More than one. :-) "$t =~ $myrand" should be "$t == $myrand" > mind that ($x modulo 35) is a number between 0 and 34 (inclusive). > > If you want a random number from 1..35 you should do this: > > $randnum = 1 + int(rand 35); > > And if you want 6 unique : > > my @myarr; > { # this block is here just to restrict the availability > # of %seen > my %seen; > while (@myarr < 6) { > my $num = 1 + int(rand 35); > push @myarr, $num > unless $seen{$num}++ > } > } > > if you wanted that a bit longer: > > my @myarr; > { # this block is here just to restrict the availability > # of %seen > my %seen; > while (@myarr < 6) { > my $num = 1 + int(rand 35); > if (! $seen{$num}) { > push @myarr, $num; > $seen{$num} = 1 > } > } > } Because of the nature of "randomness" this algorithm could take a long time to run. Better to shuffle an array of 35 numbers and pick six. my @myarr = ( 1 .. 35 ); for ( my $i = @myarr; --$i; ) { my $j = int rand( $i + 1 ); next if $i == $j; @myarr[ $i, $j ] = @myarr[ $j, $i ]; } @myarr = splice @myarr, rand( @myarr - 6 ), 6; print "@myarr\n"; John -- use Perl; program fulfillmentThread Previous | Thread Next