xsl-list
[Top] [All Lists]

Re: [xsl] generating unique random numbers

2010-02-22 11:14:37
Thank you for all your responses.

What I did was to call math:random() twice and adding the results each
time around. And I used distinct-values() everytime.

Do you think this is a good approach?



On Mon, Feb 22, 2010 at 8:21 AM, Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com> wrote:
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.xsl?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="."/>&#xA;
   </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(_dot_)online(_at_)xs4all(_dot_)nl> 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(_at_)gmail(_dot_)com> 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.

--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--





--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--