Re: [xsl] Random number seed generation

Subject: Re: [xsl] Random number seed generation
From: TW <zupftom@xxxxxxxxxxxxxx>
Date: Fri, 10 Dec 2010 00:38:08 +0100
I don't know whether this would give reasonably results, but what
about generating a seed from the result of generate-id()?  Maybe like
so, calculating kind of a digit sum (based on the the order the
variables occur in "name-chars"):

<template name="create-seed">
  <param name="string" select="generate-id(.)"/>
  <param name="preliminary-seed" select="0"/>
  <param name="maximum-value" select="100"/>

  <variable name="name-chars"
select="':ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-.0123456789'"
/>

  <choose>
    <when test="string-length($string) != 0">
      <call-template name="create-seed">
        <with-param name="string" select="substring($string,2)"/>
        <with-param name="preliminary-seed"
          select="($preliminary-seed + string-length(

substring-before($name-chars,substring($string,1,1))
                                       )
                  ) mod $maximum-value"/>
        <with-param name="maximum-value" select="$maximum-value"/>
      </call-template>
    </when>
    <otherwise>
      <value-of select="$preliminary-seed"/>
    </otherwise>
  </choose>
</template>


Of course this only makes sense if you need at most one random number
per source document node (after all, the random number will be the
same if the seed is the same).  Or you can pass the old seed as
parameter "preliminary-seed" when you create a new seed.

Thomas W.


2010/12/9 Michel Hendriksen <michel.hendriksen@xxxxxxxxx>:
> That is not an issue as that would be fixed easily. But I need to call
> the random functions from different places and that code would have to
> keep track of the last generated number. Also, when scaling the value
> (p.e.) to boolean I would need the original unscaled value for the
> next call. So results from any functions like that would have to
> return (combined) two values or would need two calls, one for the
> random number and one for the scaling.
>
> So time in millis from 1970 something would be a nice 'seed' and much
> easier to work with.
>
> Michel
>
> On Thu, Dec 9, 2010 at 4:58 PM, Tony Graham
> <Tony.Graham@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>> On Thu, Dec 09 2010 14:04:57 +0000, michel.hendriksen@xxxxx wrote:
>>> I'm using Saxon9he processor XSLT 2.0, and I am trying to get some
>>> random numbers. I'm currently using fxsl/random for this. The problem
>>> is that it needs a seed.
>>> I tried using current time stuff but that doesn't change during the
>>> transfomation using the stylesheet.. So I tried an extension function
>>> to cal to Java but that doesn't seem to work. Examples are probably
>>
>> That would require "Extensibility using reflexion", which isn't in Saxon
>> 9 HE.  See http://www.saxonica.com/feature-matrix.xml (ideally after it
>> has a stylesheet PI added to it).
>>
>>> XSLT 1.0 but should be ok.
>>>
>>> Is this possible on Home Edition? Did things change in transition to
>>> 2.0? Is there an other way to get some seed for the random function? I
>>
>> More in the transition from 'B' to 'HE', IIRC.
>>
>>> can't resuse the value as I would have to keep it somewhere in between
>>> calls. One idea would be to use generate-id() but that gives a string.
>>> Could be useful when this is always convertible to a number.
>>>
>>> I'm running the processor from a Java application now. It is a
>>> learning project and should be able to run Genetic Algorithm
>>> functionality, so lots of random stuff is needed.
>>
>> Just pass a seed generated with Java as a stylesheet parameter.
>>
>> Regards,
>>
>>
>> Tony Graham                         Tony.Graham@xxxxxxxxxxxxxxxxxxxxxx
>> Director                                  W3C XSL FO SG Invited Expert
>> Menteith Consulting Ltd                               XML Guild member
>> XML, XSL and XSLT consulting, programming and training
>> Registered Office: 13 Kelly's Bay Beach, Skerries, Co. Dublin, Ireland
>> Registered in Ireland - No. 428599   http://www.menteithconsulting.com
>>  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
>> xmlroff XSL Formatter                               http://xmlroff.org
>> xslide Emacs mode                  http://www.menteith.com/wiki/xslide
>> Unicode: A Primer                               urn:isbn:0-7645-4625-2

Current Thread