[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: generating unique random numbers
Hi Abel, > course. But Dimitre's XSLT-only approach to pseudo-random numbers in FXSL, > combined with distinct-values() will probably be a better course of action. No need to add anything to the random - generating functions of FXSL. There is already one: Templates: _ _randomRecursiveIndex and _permutationFromRecursiveIndex Here is a fragment from the test file testRandomList.xsl, which can be fuuly viewed at: http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/Tests/testRandomList.x sl?revision=1.2&view=markup Random Recursive Index 10: <xsl:variable name="vrtfRecIndex"> <xsl:call-template name="_randomRecursiveIndex"> <xsl:with-param name="pList" select="1 to 10"/> </xsl:call-template> </xsl:variable> <xsl:variable name="vRecIndex" select="$vrtfRecIndex/*"/> <xsl:for-each select="$vRecIndex"> <xsl:copy-of select="."/>
 </xsl:for-each> Randomized 10-elements list: <xsl:call-template name="_permutationFromRecursiveIndex"> <xsl:with-param name="pList" select="1 to 10"/> <xsl:with-param name="pRecIndex" select="$vRecIndex"/> </xsl:call-template> And this produces the following part of the output: Random Recursive Index 10: <el>3</el> <el>9</el> <el>2</el> <el>6</el> <el>5</el> <el>1</el> <el>2</el> <el>3</el> <el>2</el> <el>1</el> Randomized 10-elements list: <el>3</el> <el>10</el> <el>2</el> <el>8</el> <el>7</el> <el>1</el> <el>5</el> <el>9</el> <el>6</el> <el>4</el> Anyone can use this *now* (or up to four years ago) to get a randomized permutation of the set [1 .. N] -- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk ------------------------------------- Never fight an inanimate object ------------------------------------- You've achieved success in your field when you don't know whether what you're doing is work or play On Mon, Feb 22, 2010 at 2:36 AM, Abel Braaksma <abel.online@xxxxxxxxx> wrote: > As an alternative to the already proposed solutions, consider looking up the > pseudo-random method to create GUIDs in XSLT, which I cooked up a few years > ago: http://markmail.org/message/4zxb654yeaf5twnv > > It is random enough between runs (and sequential, yet non-repeating, in one > run), but maybe not random enough for you. You can elaborate on the code, of > course. But Dimitre's XSLT-only approach to pseudo-random numbers in FXSL, > combined with distinct-values() will probably be a better course of action. > > Kind regards, > Abel Braaksma > > Wolfgang Laun wrote: >> >> What you describe is not a sequence of random numbers; a rule such as >> "no duplicates" contradicts most definitions of "randomness". What you >> request is better described as "a random permutation of numbers". This >> could >> be produced by random draws from a set of numbers (without returning) >> provided that you can, somehow, define this set. Can you? >> >> I regret that I'm not experienced enough to propose an XSLT solution >> for such an algorithm. >> >> -W >> >> On Sun, Feb 21, 2010 at 3:02 AM, a kusa <akusa8@xxxxxxxxx> wrote: >> >>> >>> Hi >>> >>> I have been raking my brains to achieve this. I am using math.random() >>> to generate random numbers, but of course since they repeat, I want to >>> be able to generate unique random numbers. Can anybody please help me >>> here? How can I generate unique random numbers in xslt? >>> >>> Thanks in advance for all your support.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|