Pseudo-randomness

Uses
- Tuple items
- Integers

Seeds

(Pseudo-)random series in Leibniz are deterministic. They are computed from a seed value. If you use the same seed value, you get the same series.

Seeds are non-zero bit patterns, represented as natural numbers in Leibniz:
ℕ.nzrandomSeed

Note: this subsort definition is not strictly correct: valid seeds are fixed-size unsigned non-zero integers, i.e. a subset of ℕ.nz. The random number generator implemented in LzRandom Object subclass: #LzRandom instanceVariableNames: 'signature seed' classVariableNames: '' package: 'Leibniz2-Random' uses 256-bit seeds. Describing this properly in Leibniz' sort system would require a distinct sort hierarchy for fixed-size integers, which is not yet done.

The bits of a seed should ideally be chosen randomly. Small integer literals are therefore not good choices, having most bits set to zero. For convenience, you can get a good seed from any integer (including a small one) via
seed() : randomSeed
seed(n:) ⇒ Pharo:"(LzRandomTerms signature: signature) seedFromSmallInteger: n"

Random series

The construction of a deterministic pseudo-random series from an initial seed produces at each iteration a new value in the series and an updated seed from which the following values are computed. In the following, we call the values of the series its elements , and the combinations of values and seeds its items .

A series of random values of sort 𝕊 has sort RandomSeries(𝕊). Its elements are obtained by indexing with positive integer indices:
RandomSeries(s:𝕊)[ℕ.nz] : s

An item of a random series is the tuple combining each element with the random seed for the continuation of the series:
randomSeriesItem(RandomSeries(s:𝕊), ) : (s, randomSeed)

An element can thus be obtained as the first value in an item:
s_:𝕊, series:RandomSeries(s_), series[n:ℕ.nz] ⇒ first(randomSeriesItem(series, n))

Example

See Random integers