# The Random interface

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

    INTERFACE Random;

    TYPE
      T = OBJECT METHODS
        integer(min := FIRST(INTEGER);
          max := LAST(INTEGER)): INTEGER;
        real(min := 0.0e+0; max := 1.0e+0): REAL;
        longreal(min := 0.0d+0; max := 1.0d+0): LONGREAL;
        extended(min := 0.0x+0; max := 1.0x+0): EXTENDED;
        boolean(): BOOLEAN
      END;
      Default <: T OBJECT METHODS
        init(fixed := FALSE): Default
      END;
    END Random.

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.real(a, b)` returns a uniformly distributed `REAL` in the half-open interval `[a..b)`.

The call `longreal` and `extended` are like `real`, but return values of the specified types.

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.

#### Example.

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

#### SRC Modula-3 implementation details.

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] ).