
2bevil1
New User
Mar 6, 2008, 1:06 PM
Post #4 of 7
(5138 views)
|
Sorry. I will clarify, the unique numbers need to be displayed as two digits 01-47. The following code does it but assigns the random number to a hash to make them unique. I do not want to assign the number to a hash to do this. Near the bottom I have some code that my instructor gave me but it is not working. #!/usr/bin/perl my @numbers = (1..47); my $limit = 5; # A hash used to make sure we have # all unique numbers in the final list. my %list = (); # Here we generate the list of random # unique numbers. The hash continues to # build until there are $limit number # of keys. Since hash keys must be unique # we are left with all unique numbers # without having to check anything except the # total number of keys. while (keys %list < $limit) { $list{$numbers[rand @numbers]}=1; } # print sorted (ascending) list of numbers print join(' ', sort {$a <=> $b} keys %list); My instuctor gave me this code to use in my assignment, but I can not get it to work. I need to generate random lotto numbers. #!/usr/bin/perl -w @x=uniq(5, 10, 20); sub uniq($$$) { my ($n, $a, $b)=@_; my $str="0"x($b-$a+1); my $c=0; my @x=(); while($c < $n) { $num = int(rand($a, $b)); if(substr($str, $num-$a,1)=="0") { $x[$c++]=$num; } } return (@x) } Any help you can give me is much appriciated. Im very basic. Thanks in advanced.
|