[ Arithmetic | Reference Manual | Alphabetic Index ]

# random(+Limit, -N)

# -N is random(+Limit)

Generates a random integer N between 0 and Limit-1

Limit
  Positive Integer
N
  Integer or Variable.

## Description

Generates a pseudo-random integer between 0 and Limit-1, and unifies it with N. The numbers generated are uniformly distributed.

The algorithm used depends on the setting of the 'random_mode' flag (see set_flag/2, get_flag/2):

lcg (default)
  Use a Linear Congruential Generator with a period of 2^31-1. The sequence is reproducible independently of OS and HW. The generator state is local to the calling engine.
xs
  Use a xorshift64* generator with a period of 2^64-1. The sequence is reproducible independently of OS and HW. The generator state is local to the calling engine.
system
  Use the random() or rand() library function. The sequence may differ on different machines or OSs. The generator state is shared with the whole process.

If it is required that the sequence produced by successive calls of random/2 be reproducible, seed(Seed) can be called to initialise the generator state.

### Modes and Determinism

- random(+, -) is det

### Exceptions

(4) instantiation fault
  Limit is uninstantiated.
(5) type error
  Limit is instantiated, but not to an integer.
(6) out of range
  Limit is an integer outside the range 1..2^31

## Examples

    Success:
          ?- random(100, X).
          X = 46
          yes.

          ?- X is random(100).
          X = 36
          yes.

          ?- seed(1), X is random(100).
          X = 71
          yes.
          ?- seed(1), X is random(100).
          X = 71
          yes.

## See Also

frandom / 1, seed / 1, get_flag / 2, set_flag / 2