Re: xsl:sort with a randomize ability ? or something

Subject: Re: xsl:sort with a randomize ability ? or something
From: fmabry <fmabry@xxxxxxxxxx>
Date: Fri, 10 Nov 2000 18:56:44 -0500
By virtue of the transform language's design goals, XSLT is not
something you are supposed to ask for different results from when the
same input is provided.  There are not supposed to be any side effects.
However, some of us use the language for some things I don't think the
XML gods ever thought the mind of man would conceive.  I had a similar
problem/concern in a return on investment information security model I
developed.  Eventually I found I did not need the solution but I think
that it should meet your needs.  (For my needs I used the xsl:number of
a tree element (at all levels) as an element [ index ] into a set of
numbers accessed via document() and saved in a global variable at the
beginning of each run.)

Solution 1: import a XML random number seed set file:

Take the general procedural language of your choice (if you use Perl you
can do an immediate execution from the command line) to generate an XML
text file of numbers.  There should be as many numbers in the file as
you could have questions (note: you might have a different number of
answer elements for each question and likely more than one).

Assume your generated file is roughly like the following:


Use the document function to access the file in your XSLT and set a
global variable ($RNS) to the RNS data above.

Using the 'xsl:number' element to establish an index for each of your
question elements ($Index).

You then use the value of $RNS/rn[$Index] as the seed (call it $seed)
for a random number stream for the specific question you are
constructing.  If the number of answers for a particular question is
$NumAns, then use the first answer (from your input) as the (($seed  +
1) mod ($NumAns)) +1) answer output element.  Use the second input
answer as the (($seed  +  2) mod ($NumAns)) +1) answer output element.

Solution 2: pass a parameter from the command line or use a time based
function to provide an initial random number seed ($irns).  Get the seed
to be used for a particular question's answers by taking the question's
xsl:number element value times the initial random number seed (call the
new question seed $qs).  To retrieve the ith output answer (from among
the answer[1], answer[2],...answer[$NumAns] elements of the current
question) select='answer[ ((( $qs + $i ) mod $NumAns ) + 1)]'.

The second solution will work best if the IRNS is a prime number greater
than the number of questions and is changed each time you run the
question generation transform.

Either approach will choose different starting answers for each question
each time a different seed set or initial random number seed is

Hope this helps.


>> From: Robert Koberg
>> Date: Fri, 10 Nov 2000 07:41:34 -0800
>> I need to match a node set that would represent the answers to a mulitple
>> choice question.  It so happens that all of the answers are stored and given
>> to me with the correct answer always being first.
>> Is there a way to randomize them so people don't start to see a pattern in
>> the test? :)
Dr. Frank Mabry
Dept. of EE&CS
U.S. Military Academy
West Point, New York, 10996

Work Phone: 914-938-2960
work email: df6954@xxxxxxxxxxxxxxx
home email: fmabry@xxxxxxxxxx

 XSL-List info and archive:

Current Thread