in reply to Select three random numbers between 1..10
A better method is something like this:
At the cost of storing all the numbers you are likely to generate, this code guarantees that it will finish in time proportional to n (instead of time growing at the same rate as n/m where n is the number of numbers chosen, and m is the size of the set).@arr=1..10; for (1..3) { $i=int(rand(scalar @arr)); push(@num,splice(@arr,$i,1)); } foreach my $num (@num){ print"$num\n"; }
|
|---|