Re: [xsl] Random number seed generation

Subject: Re: [xsl] Random number seed generation
From: Michel Hendriksen <michel.hendriksen@xxxxxxxxx>
Date: Fri, 10 Dec 2010 15:17:44 +0100
Got it working on HE now but still problem with function-available()
from the B-type solution (in separate topic).

Nice sample here for interested people:

http://www.vijedi.net/2010/creating-a-custom-xslt-function-in-saxon-he/

On Fri, Dec 10, 2010 at 1:18 PM, Michel Hendriksen
<michel.hendriksen@xxxxxxxxx> wrote:
> Or:
>
>  <with-param name="preliminary-seed"
>  select="($preliminary-seed +
>              10*string-length(
>                substring-before($name-chars,substring($string,1,1))
>              )
>            ) mod $maximum-value"/>
>
>
> I got this working now for SaxonB:
>
> <xslt:template name="seed">
>        <!-- Works with SaxonB only! -->
>        <xslt:choose>
>                <xslt:when test="function-available('date:new') and
> function-available('date:get-time')" xmlns:date="java:java.util.Date">
>                        <xslt:variable name="date" select="date:new()"/>
>                        <xslt:value-of select="date:get-time($date)"/>
>                </xslt:when>
>                <xslt:otherwise>
>                        <!-- NOT random at all..... -->
>                        <xslt:variable name="time" select="current-time()"
/>
>                        <xslt:value-of select="seconds-from-time($time) + 60
*
> minutes-from-time($time) + 3600 * hours-from-time($time)"/>
>                </xslt:otherwise>
>        </xslt:choose>
> </xslt:template>
>
> Gonna look into extensions for Saxon 9.3HE.
>
> Michel
>
> On Fri, Dec 10, 2010 at 1:15 PM, TW <zupftom@xxxxxxxxxxxxxx> wrote:
>> There is certainly a lot of room for improvement.  My template returns
>> the same number if two ids consist of the same characters in a
>> different order.  Maybe multiplying with a constant in every step
>> would already be a bit better:
>>
>> <with-param name="preliminary-seed"
>>  select="3*($preliminary-seed +
>>              string-length(
>>                substring-before($name-chars,substring($string,1,1))
>>              )
>>            ) mod $maximum-value"/>
>>
>>
>> 2010/12/10 TW <zupftom@xxxxxxxxxxxxxx>:
>>> 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.

Current Thread