[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Random number seed generation

Subject: Re: Random number seed generation
From: Michel Hendriksen <michel.hendriksen@xxxxxxxxx>
Date: Fri, 10 Dec 2010 13:18:36 +0100
Re:  Random number seed generation
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.