xsl-list
[Top] [All Lists]

Re: [xsl] How to Do Random "Shuffle"?

2014-09-18 06:14:13
random:random-sequence(N)

If this is supposed to produce a sequence of numbers in the range 1..N
while expecting it to contain every number of that range exactly once:
would this truly be a "random" sequence? I don't think so.

-W



On 18 September 2014 11:05, David Rudel fwqhgads(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

When I have to do this (essentially create a permutation of the numbers
from 1 to N), I combine random:random-sequence with saxon:sort

I'm away right now so I'm not working on a machine with XSLT, so the
following syntax may be off, but I use:

<xsl:variable name="rand" select="random:random-sequence(N)"/>

<xsl:variable name="sorted.rand" select="saxon:sort($rand)"/>

<xsl:variable name="permutation" select="$rand!index-of($sorted.rand,.)"/>

The select attribute of the last can also be written as "for $i in $rand
return index-of($sorted.rand,$i)"  .


On Saturday, September 13, 2014, Eliot Kimber ekimber(_at_)contrext(_dot_)com 
<
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Using XSLT 2 I need to implement rendering of "match table" questions
where you have two sets of items, the match item and the thing it matches
to. I want to present this as a literal table, where the first column is
the match-from items in source order and the second column is the match-to
items, in random order.

I think this is best characterized as a "shuffle" problem, where you want
to reorder a list randomly but all items in the list must be accounted
for.

I can think of a recursive algorithm: given a list, generate a random
integer between 1 and the list length, select that item and add it to the
result list, then call this function on the original list minus the node
you just selected.

Is there an easier or more efficient way to do it?

Thanks,

Eliot
----------
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com




--

"A false conclusion, once arrived at and widely accepted is not dislodged
easily, and the less it is understood, the more tenaciously it is held." -
Cantor's Law of Preservation of Ignorance.
  XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <-list/528976> (by
email <>)

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>