[xquery-talk] Random number generation : requirements
Leonard Wörteler
leo at woerteler.de
Tue May 6 16:20:13 PDT 2014
Hello Michael,
Am 06.05.2014 18:53, schrieb Michael Kay:
> If you have any applications that use or need such a function, please could I have a brief description of the way it uses random numbers, e.g. just wanting a single random number, a random permutation of 52 integers, an arbitrary sequence of random numebrs for test data generation, etc.
in contrast to everyone else (it seems) I implemented a *deterministic*
random-number generator in XQuery [1] in order to generate reproducible
fuzzy tests for my higher-order xq-modules project [2].
I used a simple Linear Congruential Generator [3] and implemented the
following functions:
rng:with-random-ints(
$seed as xs:integer,
$n as xs:integer,
$start as item()*,
$f as function(item()*, item()*) as item()*
)
realizes a constant-space fold over `$n` randomly generated `xs:int`s
with initial seed `$seed`. This is used by
rng:random-ints(
$seed as xs:integer,
$n as xs:integer
) as xs:int* {
rng:with-random-ints($seed, $n, (), function($seq, $i) {
($seq, $i)
})
};
to generate a sequence of random `xs:int` values. The module is used in
some tests [4,5].
You could obviously also implement `rng:with-random-ints(...)` in terms
of `rng:random-ints(...)`:
rng:with-random-ints($seed, $n, $start, $f) {
fn:fold-left(rng:random-ints($seed, $n), $start, $f)
};
-- Leo
[1]
https://github.com/LeoWoerteler/xq-modules/blob/master/src/main/xquery/modules/rng.xqm
[2] https://github.com/LeoWoerteler/xq-modules
[3] http://en.wikipedia.org/wiki/Linear_congruential_generator
[4]
https://github.com/LeoWoerteler/xq-modules/blob/master/src/test/xquery/int-set-test.xq#L33-51
[5]
https://github.com/LeoWoerteler/xq-modules/blob/master/src/test/xquery/heap-test.xq#L10
More information about the talk
mailing list