XQuery get Random Text from a List

纵然是瞬间 提交于 2020-01-05 03:30:11

问题


Suppose I have a list of 100 String Element and I want to get 50 of these random Text string to be returned Randomly.

I try to do this:

let $list := ("a","b",..."element number 100")
return xdmp:random(100)

This query return one string, I want to return back 50 strings that are distinct from each other.


回答1:


Easiest to order by xdmp:random() and limit to the first 50:

(for $x in (1 to 100)
order by xdmp:random()
return $x
)[1 to 50]



回答2:


xdmp:random() (as well as xdmp:elapsed-time()) return different values at each call. It would be rather impractical if it wouldn't. This opposed to for instance fn:current-dateTime() which gives the same value throughout one execution run.

Ghislain is making a good first attempt, but as also pointed out by BenW, even though xdmp:random() does return different results each time, it is not said they are unique throughout one execution run. Collisions on its 64-bit max scale are rare (though still possible), but on small scale like 10's or 100's it is likely to have some accidental repetition. It is wise to eliminate the texts from the list once chosen.

BenW beat me to posting an alternative to Ghislain, and it looks similar, but uses less lines. Posting it anyhow, in the hope someone finds it useful:

declare function local:getRandomTexts($list, $count) {
  if ($count > 0 and exists($list)) then
    let $random := xdmp:random(count($list) - 1) + 1
    let $text := $list[$random]
    return ($text, local:getRandomTexts($list[. != $text], $count - 1))
  else ()
};

let $list :=
  for $i in (1 to 26)
  return fn:codepoints-to-string(64 + $i)
for $t in local:getRandomTexts($list, 100)
order by $t
return $t

HTH!




回答3:


If you are saying the 50 strings must be distinct from one another, as in, no repeats allowed, then even if xdmp:random() does return different values when called repeatedly in the same query, getting 50 random positions in the same list is not sufficient, because there may be repeats. You need to get random positions from 50 different lists.

declare function local:pickSomeFromList($some as xs:integer, $listIn as xs:string*, $listOut as xs:string*) as xs:string* {
  if($some = 0 or not($listIn)) then $listOut
  else 
    let $random := xdmp:random(count($listIn) - 1) + 1
    return local:pickSomeFromList(
      $some - 1, 
      ($listIn[fn:position() lt $random],$listIn[fn:position() gt $random]), 
      ($listOut, $listIn[$random])
    )
};
let $list := ("a","b","c","d","e","f","g","h","i","element number 10")
return local:pickSomeFromList(5, $list, ())



回答4:


Assuming it returns a different result at every call (but I cannot tell from the documentation of xdmp:random whether it is the case), the following code returns 50 strings from the list picked at random (but not necessarily distinct):

let $list := ("a","b",..."element number 100")
for $i in 1 to 50
let $position = 1 + xdmp:random(99)
return $list[$position]

However, the exact behavior of xdmp:random, that is, whether it returns identical results across calls, depends on how the MarkLogic engine supports or treats nondeterministic behavior, which is outside of the scope of the XQuery specification. Strict adherence to the specification would actually return 50 times the same result with the above query.

XQuery 3.1 provides a random number generator with which you can control the seed. This allows you to generate as many numbers as you want by chaining calls, while only using interoperable behavior and staying within a fully deterministic realm.

Edit: here is a query (still assuming calls to xdmp:random are made each time) that should make sure that 50 distinct strings from the list are taken following grtjn's remark. It uses a group by clause and relies on a lazy evaluation for taking the first 50.

let $list := ("a","b",..."element number 100")
let $positions := (
    for $i in 1 to 100000 (: can be adjusted to make sure we get 50 distinct :)
    group by $position = 1 + xdmp:random(count($list) - 1)
    return $position
  )[position() le 50]
return $list[position() = $positions]

I think hunterhacker's proposal for computing the $positions is even better though.



来源:https://stackoverflow.com/questions/42250103/xquery-get-random-text-from-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!