xsl-list
[Top] [All Lists]

Re: [xsl] Random number seed generation

2010-12-10 06:15:42
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(_at_)googlemail(_dot_)com>:
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.


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--