[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Random number seed generation


Subject: Re: [xsl] Random number seed generation
From: TW <zupftom@xxxxxxxxxxxxxx>
Date: Fri, 10 Dec 2010 13:15:24 +0100

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