http://zorba.io/modules/random

View as XML or JSON.

This module provides several functions for generating (pseudo-)random numbers and strings.

Function Summary

random-between ($lower as integer, $upper as integer) as integer

This function generates one random number within a given range.

random-between ($lower as integer, $upper as integer, $num as integer) as integer*

This function generates an arbitrary number of random numbers within a given range.

random () as integer

This function generates one random number.

random ($num as integer) as integer* external

This function generates an arbitrary number of random numbers.

seeded-random-between ($seed as integer, $lower as integer, $upper as integer, $num as integer) as integer*

This function generates an arbitrary number of pseudo-random numbers within a given range.

seeded-random ($seed as integer, $num as integer) as integer* external

This function generates an arbitrary number of pseudo-random numbers.

uuid () as string external

This function returns a uuid.

Functions

random-between#2

declare  %an:nondeterministic function r:random-between($lower as integer, $upper as integer) as integer

This function generates one random number within a given range. The function is nondeterministic.

The function is based on r:random-between#3. Specifically, it returns the value of invoking r:random-betwen($lower, $upper, 1).

Parameters

lower as integer
the lower bound for the random number
upper as integer
the upper bound for the random number

Returns

integer
a random integer within the given range

random-between#3

declare  %an:nondeterministic function r:random-between($lower as integer, $upper as integer, $num as integer) as integer*

This function generates an arbitrary number of random numbers within a given range. The function is nondeterministic because the sequence is not repeatable.

Parameters

lower as integer
the lower bound for every value within the sequence
upper as integer
the upper bound for every value within the sequence
num as integer
the length of the sequence returned

Returns

integer*
$num pseudo-random integers within (and including) the range specified by $lower and $upper. It returns $num-times $lower if $lower is equal to $upper and the empty sequence if $num is negative.

random#0

declare  %an:nondeterministic function r:random() as integer

This function generates one random number. The function is nondeterministic.

The function is based on r:random#1. Specifically, it returns the value of invoking r:random(1).

Parameters

Returns

integer
a random integer

random#1

declare  %an:nondeterministic function r:random($num as integer) as integer* external

This function generates an arbitrary number of random numbers. The function is nondeterministic because the sequence is not repeatable.

However, the function is based on posix function srand() and rand(). Specifically, it invokes srand() with some random number based on the current time and then returns the values returned by invoking rand() $num-times.

Parameters

num as integer
the length of the sequence returned

Returns

integer*
$num random integers, or the empty sequence if $num is negative.

seeded-random-between#4

declare  function r:seeded-random-between($seed as integer, $lower as integer, $upper as integer, $num as integer) as integer*

This function generates an arbitrary number of pseudo-random numbers within a given range. The sequence is repeatable by calling the function with the same seed and boundaries.

The function is based on the function r:seeded-random#2. Specifically, its result is repeatable if called with the same arguments.

Parameters

seed as integer
the initial seed value for the sequence
lower as integer
the lower bound for every value within the sequence
upper as integer
the upper bound for every value within the sequence
num as integer
the length of the sequence returned

Returns

integer*
$num pseudo-random integers within (and including) the range specified by $lower and $upper. It returns $num-times $lower if $lower is equal to $upper and the empty sequence if $num is negative.

seeded-random#2

declare  function r:seeded-random($seed as integer, $num as integer) as integer* external

This function generates an arbitrary number of pseudo-random numbers. The sequence is repeatable by calling the function with the same seed.

The function is based on posix function srand() and rand(). Specifically, it invokes srand($seed) and then returns the values returned by invoking rand() $num-times.

Sequences returned by this function are not thread-safe (i.e. if multiple XQuery programs invoking this function are executed concurrently in several threads). This is because the function is based on srand() and rand().

Parameters

seed as integer
the initial seed value for the sequence
num as integer
the length of the sequence returned

Returns

integer*
$num pseudo-random integers, or the empty sequence if $num is negative.

uuid#0

declare  %an:nondeterministic function r:uuid() as string external

This function returns a uuid.

Note, that the function is not stable, that is, it returns a different UUID everytime the function is invoked.

Parameters

Returns

string
the generated UUID as string