Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

initstate, random, setstate, srandom - generate pseudo-random numbers

&pagelevel(4)&pagelevel

Syntax

#include <stdlib.h>

char *initstate(unsigned int seed, char *state, size_t size);

long random(void);

char *setstate(const char *state);

void srandom(unsigned int seed);

Description random() uses a non-linear, additive feedback random-number generator and uses a

standard status array with the size of 31 long integers to generate successive pseudo-random numbers in the range 0 through 231-1. The period of this random-number generator is very large - approximately 16 x (231-1). The size of the status array determines the period of the random-number generator. If a larger status array is used, the period is extended.

With 256-byte status information the period of the random-number generator is greater than 269.

Like rand(), random() generates by default a sequence of numbers which can be duplicated by calling srandom() with seed equals 1 beforehand.

srandom() initializes the current status array with the contents of seed.

The initstate() and setstate() functions handle the restart and the modification of random-number generators. With initstate() the status vector pointed to by the state argument can be initialized for later use. The size argument specifies the size of the status vector in bytes. initstate() uses size to establish how demanding the random-number generator used is to be - the more status information, the better the random numbers generated. Optimum values for the amount of status information are 8, 32, 64, 128 and 256 bytes; other specifications > 8 are rounded down to the next lowest of these values. For values < 8, random() uses a simple, linear, congruent random-number generator. The seed argument determines the start value for the initialization via which a starting point for the random-number sequence is specified which also serves simultaneously for a restart. initstate() returns a pointer to the previous array with status information.

If random() is called without initstate() having been executed beforehand, random() behaves as if initstate() had been executed with seed=1 and size=128 beforehand.

Once a status has been initialized, the setstate() function allows a quick change of the status arrays. The status array pointed to by state is used for the generation of further random numbers until the next call of initstate() or setstate(). setstate() returns a pointer to the previous status array.

inistate() is not thread-safe. Use the reentrant function rand_r() when needed.

Return val.

random():

Pseudo-random number

The function is always successful.

initstate() and setstate():

Pointer to the previous status array


if successful.

Null pointer

if an error occurs.

Notes

After a status array has been initialized, it can be restarted in a different place:

  • by calling initstate() with the desired start value, the status array and its size.

  • by calling setstate() with the status array, followed by srandom() with the desired
    start value. The advantage of calling these two functions is that the size of the status
    vector does not need to be stored after its initialization.

Example

The following statements initialize a status array, transfer an initstate() and output a
random number generated with random():

static long state1[32] = { 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 
0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, 
0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 
0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 0xde3b81e0, 0xdf0a6fb5, 
0xf103bc02, 0x48f340fb, 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 
0xf5ad9d0e, 0x8999220b, 0x27fb47b9 };
main()
{ 
   unsigned seed;
   int n; 
   seed = 1; 
   n = 128;
   initstate(seed, state1, n); 
   setstate(state1);
   printf("%d", random());
}

See also

drand48drand48(), rand(), rand_r(), srand(), stdlib.h.