xsl-list
[Top] [All Lists]

Re: [xsl] Random number generation : requirements

2014-05-06 12:22:32
I recently used the EXSLT random number extension in Saxon to generate
random numbers within a range from 0 to say 20,000 in order to randomly
select items from a document (in my case, words from the standard Linux
words spellcheck dictionary, made into a simple XML document for ease of
XSLT access).

This was the relevant code I used:

<xsl:variable name="words" as="document-node()"
select="document('data-files/words.xml')"/>
  <xsl:variable name="wordCount" as="xs:integer"
select="count($words/*/*)"/>


<xsl:variable name="seed"
select="(seconds-from-dateTime(current-dateTime()) + .) * 1000"
as="xs:double"/>


<xsl:variable name="wordIndex" as="xs:integer"
        select="if ($doRandom)
        then xs:integer(exslt-random:random-sequence(1, $seed) *
$wordCount)
        else ."
        />

The seed construction is a little weak but sufficient for my needs since I
wouldn't be running the process twice within the same second.


Of course, being able to get a truly random seed would be nice but
pseudo-random is usually good enough.

Cheers,

Eliot

—————
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com




On 5/6/14, 11:54 AM, "Michael Kay mike(_at_)saxonica(_dot_)com"
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

The W3C WGs are looking at the idea of introducing a random-number
function of some kind in XPath 3.1. The challenge of course is making
this both usable and a pure function with no side-effects. We have
various design ideas which we need to test against requirements.

If you have any applications that use or need such a function, please
could I have a brief description of the way it uses random numbers, e.g.
just wanting a single random number, a random permutation of 52 integers,
an arbitrary sequence of random numebrs for test data generation, etc.

If you're currently using the EXSLT random-sequence() function, please
share your experience with it.

Michael Kay
Saxonica


--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>