RSS 2.0
Sign In
# Saturday, 22 November 2008

Recently, working on completely different thing, I've realized that one may create a "generator", function returning different values per each call. I was somewhat puzzled with this conclusion, as I thought xslt functions have no side effects, and for the same arguments xslt function returns the same result.

I've confirmed the conclusion at the forum. See Scope of uniqueness of generate-id().

In short:

  • each node has an unique identity;
  • function in the course of work creates a temporary node and produces a result depending on identity of that node.

Example:

<xsl:stylesheet version="2.0"
  xmlns:f="data:,f"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:message select="
    for $i in 1 to 8 return
      f:fun()"/>
</xsl:template>

<xsl:function name="f:fun" as="xs:string">
  <xsl:variable name="x">!</xsl:variable>

  <xsl:sequence select="generate-id($x)"/>
</xsl:function>

</xsl:stylesheet>

The next thought was that if you may create a generator then it's easy to create a good random number generator (that's a trivial math task).

Hey gurus, take a chance!

Saturday, 22 November 2008 08:27:48 UTC  #    Comments [2] -
xslt
Monday, 01 December 2008 01:48:04 UTC
To create a *good* random generator is not a trivial task.

There are pure XSLT random generation functions in FXSL. The initial seed has to be provided as a (global) parameter.

Do note, that there is nothing like a "random number" in singular. Any single number is "random". We can only speak about "randomness" as a property of a sufficiently long sequence of numbers. This is why, for example, the EXSLT library specifies a random-sequence() function, but not a random() function in its "random" module.

Cheers,
Dimitre Novatchev
Monday, 01 December 2008 04:20:43 UTC
Dimitre,

Many pseudo random algorithms require two things:
a seed;
a sequence of numbers;

You have both:
use either global parameter or current timestamp to derive a seed;
use generator to derive a sequence.

A possible disadvantage of such implementation is that the pseudo random sequence itself is not reproducible, as we cannot generally control values produced with generator. We only know they are different.
Vladimir Nesterovsky
Comments are closed.
Archive
<2008 November>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Statistics
Total Posts: 387
This Year: 3
This Month: 1
This Week: 0
Comments: 971
Locations of visitors to this page
Disclaimer
The opinions expressed herein are our own personal opinions and do not represent our employer's view in anyway.

© 2024, Nesterovsky bros
All Content © 2024, Nesterovsky bros
DasBlog theme 'Business' created by Christoph De Baene (delarou)