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


Random State Initialization

See section Random Number Functions for a discussion on how to choose the initial seed value passed to these functions.

Function: void gmp_randinit (gmp_randstate_t state, gmp_randalg_t alg, ...)
Initialize random state variable state.

alg denotes what algorithm to use for random number generation. Use one of

If alg is 0 or GMP_RAND_ALG_DEFAULT, the default algorithm is used. The default algorithm is typically a fast algorithm like the linear congruential and requires a third size argument (see GMP_RAND_ALG_LC).

When you're done with a state variable, call gmp_randclear to deallocate any memory allocated by this function.

gmp_randinit may set the following bits in gmp_errno:

Function: void gmp_randinit_lc_2exp (gmp_randstate_t state, mpz_t a,
unsigned long int c, unsigned long int m2exp)

Initialize random state variable state with given linear congruential scheme.

Parameters a, c, and m2exp are the multiplier, adder, and modulus for the linear congruential scheme to use, respectively. The modulus is expressed as a power of 2, so that @ifnottex m = 2^m2exp.

The least significant bits of a random number generated by the linear congruential algorithm where the modulus is a power of two are not very random. Therefore, the lower half of a random number generated by an LC scheme initialized with this function is discarded. Thus, the size of a random number is m2exp / 2 (rounded upwards) bits when this function has been used for initializing the random state.

When you're done with a state variable, call gmp_randclear to deallocate any memory allocated by this function.

Function: void gmp_randseed (gmp_randstate_t state, mpz_t seed)
Function: void gmp_randseed_ui (gmp_randstate_t state, unsigned long int seed)

Set the initial seed value.

Parameter seed is the initial random seed. The function gmp_randseed_ui takes the seed as an unsigned long int rather than as an mpz_t.

Function: void gmp_randclear (gmp_randstate_t state)
Free all memory occupied by state. Make sure to call this function for all gmp_randstate_t variables when you are done with them.


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