jeudi 11 mai 2023

Generate a sequence of random numbers

I am trying to generate a sequence of random numbers using the XPath 3.1 random-number-generator() function.

The specification states that the function returns a map containing three entries, and that the entry with key "next" is a zero-arity function that can be called to return another random number generator. However, there are no instructions (at least not instructions that I could understand) regarding how exactly to do that.

I was able, after some trial and error, to produce two different random numbers using:

<test>
    <xsl:value-of select="random-number-generator()?number" />
    <xsl:text> - </xsl:text>
    <xsl:value-of select="random-number-generator()?next()?number" />
</test>

However, adding a third call using the same code as the second one:

<test>
    <xsl:value-of select="random-number-generator()?number" />
    <xsl:text> - </xsl:text>
    <xsl:value-of select="random-number-generator()?next()?number" />
    <xsl:text> - </xsl:text>
    <xsl:value-of select="random-number-generator()?next()?number" />
</test>

results in the last two random numbers being equal, for example:

<test>0.8258447548324238 - 0.37162622506875487 - 0.37162622506875487</test>

(full test at https://xsltfiddle.liberty-development.net/gWmsLza).

The specification also provides an example of "a function that can be called to produce a random sequence of xs:double values in the range zero to one, of specified length". This is what I would eventually like to do, but the provided code is not in the XSLT language and I am not able to understand how it works.

Can someone please provide clear instructions how to call the function repeatedly so that it provides a new random number at each call?




Aucun commentaire:

Enregistrer un commentaire