Reputation: 271
Let us assume that we have a sequence s:=('a','b','c','d','e')
. I want to return a random sample consist of three items from this sequence. How to do so in Marklogic?
Note: The sequence that I've used is an example. I'm dealing with large sequence.
Upvotes: 2
Views: 304
Reputation: 38702
One possible way is to repeatedly pull a single value using a recursive function.
declare function local:draw-n($sequence, $n) {
if ($n > 0)
then
let $index := 1 + xdmp:random(count($sequence))
return ($sequence[$index], local:draw-n($sequence[
position() < $index or position() > $index
], $n - 1))
else
()
};
local:draw-n(('a','b','c','d','e'), 3)
There might be more efficient ways for large sequences, though (like generating three random numbers, making sure they're different and using them to select from the sequence).
declare function local:n-different-random-values($values, $n, $max) {
if (count($values) eq $n)
then $values
else
let $values := ($values, 1 + xdmp:random(count($sequence)))
return local:n-different-random-values(distinct-values($values), $n, $max)
};
let $sequence := ('a','b','c','d','e')
let $indexes := local:n-different-random-values((), 3, count($sequence))
return $sequence[position() = $indexes]
Upvotes: 4