Previous: , Up: Numbers   [Contents][Index]


9.10 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 SXEmacs, pseudo-random numbers are generated from a “seed” number. Starting from any given seed, the random function always generates the same sequence of numbers. SXEmacs always starts with the same seed value, so the sequence of values of random is actually the same in each SXEmacs run! For example, in one operating system, the first call to (random) after you start SXEmacs 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 XEmacs’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 SXEmacs’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.