Bested by Blakeley yet again!
For the sake of posterity...
---
declare function local:shuffle($seq as item()*)
{
for $i in local:randIntSeq(fn:count($seq))
return $seq[$i]
};
let $seq := ('apple', 'banana', 'cherry', 'danish', 'egg')
return local:shuffle($seq)
---
Thanks Mike, not sure I'll ever stop learning from you,
Eric
Michael Blakeley wrote:
Here's a shuffle function that will sort randomly, but repeatably (ie,
based on a seed). This is quick with a small use-case, but the longer
the list, the longer this will take to run. Query results may contain
millions of items, so it might take a while to re-sort them. But you
could reasonably call cts:uris() with a cts:query argument, then re-sort
the first N uris randomly.
declare variable $LIST as xs:string+ := (
for $i in 1 to 26
return codepoints-to-string(96 + $i)
);
declare function local:shuffle($list as xs:string*, $seed as xs:integer?)
as xs:string*
{
if (empty($list)) then $list
else
let $seed :=
if ($seed) then $seed else xdmp:random()
for $i at $x in $list
order by xdmp:hash64(string(64 * $x + xs:double($seed) * 256))
return $i
};
(: test :)
text { local:shuffle($LIST, 17) },
text { local:shuffle($LIST, 17) },
text { local:shuffle($LIST, 18) },
text { local:shuffle($LIST, ()) }
-- Mike
On 2009-06-26 14:33, Eric Palmitesta wrote:
This seems to work for me, though I'm not sure if it will apply
effectively to your situation.
---
xquery version "1.0-ml";
declare function local:randIntSeq(
$in as xs:integer*, $out as xs:integer*)
{
if (count($in) eq 0) then $out
else
let $r := xdmp:random(count($in))
return local:randIntSeq(
($in[1 to ($r - 1)], $in[($r + 1) to last()]),
($out, $in[$r])
)
};
declare function local:randIntSeq($max as xs:integer)
{
local:randIntSeq((1 to $max), ())
};
let $seq := ('apple', 'banana', 'cherry', 'danish', 'egg')
let $seqLength := fn:count($seq)
return
for $i in local:randIntSeq($seqLength)
return $seq[$i]
---
It just generates a sequence of random integers which you can use to
pull items out of your desired sequence.
Eric
Mindie Sorenson wrote:
Does anyone have any good ideas on how to return query results in a
random order?
Thanks
Mindie
NOTICE: This email message is for the sole use of the intended
recipient(s) and may contain confidential and privileged information.
Any unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
------------------------------------------------------------------------
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general