in reply to Creating an array of unique numbers
How about creating an array with 0..Number_Of_Lines_In_File, using Algorithm::Numerical::Shuffle to randomize, then taking the first 'n' slots as the indexes to the question? (Ask one of the math geeks about perhaps creating a random list of 0..$Max_Questions, and using that to select from the shuffled list. It *may* improve that randomicity. I dunno.)
This one particular run seems weighted to lower values, but a purely visual guess of subsequent runs appears to have moderately even distributions over the range.
#!/usr/local/bin/perl -w use strict; use Algorithm::Numerical::Shuffle qw(shuffle); srand(); my @array1=(); my $Max_Questions = 30; #Will vary upon how many questions #the person wants to answer my $ref = random(\@array1,$Max_Questions); sub random{ my $array = shift; my $Max = shift; my $lines = 100; #There is a function that gets the #number of lines in the actual data #file, but there will be at least 100. @$array = (shuffle (0..$lines))[0..$Max_Questions]; my @sorted = sort {$a<=>$b} @$array; print"Element\t\tUnsorted\tSorted\n"; print"-------\t\t--------\t------\n"; for(my $z = 0;$z<$Max;$z++){ print" $z"; print"\t\t @$array[$z]\t\t"; print" $sorted[$z]\n"; } }
--ChrisElement Unsorted Sorted ------- -------- ------ 0 30 0 1 48 1 2 70 3 3 55 4 4 3 6 5 23 8 6 45 10 7 1 21 8 33 22 9 31 23 10 28 26 11 0 27 12 97 28 13 6 30 14 91 31 15 10 33 16 22 36 17 36 42 18 26 43 19 43 45 20 69 48 21 27 50 22 59 55 23 8 59 24 72 65 25 65 69 26 94 70 27 42 72 28 50 91 29 4 94
|
|---|