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


Random Number Functions

There are two groups of random number functions in GNU MP; older functions that call C library random number generators, rely on a global state, and aren't very random; and newer functions that don't have these problems. The newer functions are self-contained, they accept a random state parameter that supplants global state, and generate good random numbers.

The random state parameter is of the type gmp_randstate_t. It must be initialized by a call to one of the gmp_randinit functions (section Random State Initialization). The initial seed is set using one of the gmp_randseed functions (section Random State Initialization).

The size of the seed determines the number of different sequences of random numbers that is possible to generate. The "quality" of the seed is the randomness of a given seed compared to the previous seed used and affects the randomness of separate number sequences.

The algorithm for assigning seed is critical if the generated random numbers are to be used for important applications, such as generating cryptographic keys.

The traditional method is to use the current system time for seeding. One has to be careful when using the current time though. If the application seeds the random functions very often, say several times per second, and the resolution of the system clock is comparatively low, like one second, the same sequence of numbers will be generated until the system clock ticks. Furthermore, the current system time is quite easy to guess, so a system depending on any unpredictability of the random number sequence should absolutely not use that as its only source for a seed value.

On some systems there is a special device, often called /dev/random, which provides a source of somewhat random numbers more usable as seed.

The functions actually generating random functions are documented under "Miscellaneous Functions" in their respective function class: section Miscellaneous Functions, section Miscellaneous Functions.


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