Random Number Generator


A "Random.T" (or just a generator) is a pseudo-random number generator.

Individual generators are unmonitored, and all the operations have side effects.

The methods provided by a generator "rand" are:

The call "rand.integer(a, b)" returns a uniformly distributed "INTEGER" in the closed interval "[a..b]".

The call "rand.boolean()" returns a random "BOOLEAN" value.

It is a checked runtime error if "min > max" on any call.

"NEW(Default).init()" creates and initializes a generator (see below for implementation details). If "fixed" is "TRUE", a predetermined sequence is used. If "fixed" is "FALSE", "init" chooses a random seed in such a way that different sequences result even if "init" is called many times in close proximity.

A good pseudo-random permutation of an array "a" can be generated as follows:

| WITH rand = NEW(Random.Default).init() DO
|   FOR i := FIRST(a) TO LAST(a) - 1 DO
|     WITH j = rand.integer(i, LAST(a)) DO
|       `Exchange "a[i]" and "a[j]"`
|     END
|   END
| END
The object returned by a call of "New(Default).init" uses an additive generator based on Knuth's Algorithm 3.2.2A (see Knuth:Vol2).


garrett@cs.washington.edu