setstate() — Change generator for random()

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <stdlib.h>

char *setstate(const char *state);

General description

The setstate() function allows switching between state arrays used by the random() function once a state has been initialized. The array defined by the state argument is used for further random-number generation by the calling thread until initstate() is called or setstate() is called again. The setstate() function returns a pointer to the previous state array.

After initialization, a state array can be restarted at a different point by calling setstate() with the desired state, followed by srandom() with the desired seed.

Returned value

If successful, setstate() returns a pointer to the previous state array.

If unsuccessful, setstate() returns a NULL pointer. The function will fail and write a message to standard error if it detects that the state information has been damaged.

Related information