Go to the first, previous, next, last section, table of contents.


Random Numbers

A deterministic computer program cannot generate true random numbers. For most purposes, pseudo-random numbers suffice. A series of pseudo-random numbers is generated in a deterministic fashion. The numbers are not truly random, but they have certain properties that mimic a random series. For example, all possible values occur equally often in a pseudo-random series.

In Emacs, pseudo-random numbers are generated from a "seed" number. Starting from any given seed, the random function always generates the same sequence of numbers. Emacs always starts with the same seed value, so the sequence of values of random is actually the same in each Emacs run! For example, in one operating system, the first call to (random) after you start Emacs always returns -1457731, and the second one always returns -7692030. This repeatability is helpful for debugging.

If you want truly unpredictable random numbers, execute (random t). This chooses a new seed based on the current time of day and on Emacs's process ID number.

Function: random &optional limit
This function returns a pseudo-random integer. Repeated calls return a series of pseudo-random integers.

If limit is a positive integer, the value is chosen to be nonnegative and less than limit.

If limit is t, it means to choose a new seed based on the current time of day and on Emacs's process ID number.

On some machines, any integer representable in Lisp may be the result of random. On other machines, the result can never be larger than a certain maximum or less than a certain (negative) minimum.


Go to the first, previous, next, last section, table of contents.