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"; } }
Element 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
--Chris

e-mail jcwren

In reply to (jcwren) Re: Creating an array of unique numbers by jcwren
in thread Creating an array of unique numbers by TStanley

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.