random, srandom, initstate, setstate - random number generator


   #include <stdlib.h>

   long int random(void);

   void srandom(unsigned int seed);

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

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   random(), srandom(), initstate(), setstate():
       _XOPEN_SOURCE >= 500
           || /* Glibc since 2.19: */ _DEFAULT_SOURCE
           || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE


   The  random() function uses a nonlinear additive feedback random number
   generator employing a default table of size 31 long integers to  return
   successive  pseudo-random numbers in the range from 0 to RAND_MAX.  The
   period of this random number generator  is  very  large,  approximately
   16 * ((2^31) - 1).

   The srandom() function sets its argument as the seed for a new sequence
   of pseudo-random integers to be returned by random().  These  sequences
   are  repeatable  by  calling srandom() with the same seed value.  If no
   seed value is provided, the random() function is  automatically  seeded
   with a value of 1.

   The  initstate()  function allows a state array state to be initialized
   for use by random().  The  size  of  the  state  array  n  is  used  by
   initstate()  to  decide  how sophisticated a random number generator it
   should use—the larger the state array, the better  the  random  numbers
   will  be.   seed  is the seed for the initialization, which specifies a
   starting point  for  the  random  number  sequence,  and  provides  for
   restarting at the same point.

   The  setstate()  function  changes the state array used by the random()
   function.  The state array state is used for random  number  generation
   until  the  next  call  to initstate() or setstate().  state must first
   have been initialized using initstate() or be the result of a  previous
   call of setstate().


   The  random()  function  returns  a  value between 0 and RAND_MAX.  The
   srandom() function returns no value.

   The initstate() function returns a pointer to the previous state array.
   On error, errno is set to indicate the cause.

   On  success,  setstate() returns a pointer to the previous state array.
   On error, it returns NULL, with errno set to indicate the cause of  the


   EINVAL The state argument given to setstate() was NULL.

   EINVAL A state array of less than 8 bytes was specified to initstate().


   For   an   explanation   of   the  terms  used  in  this  section,  see

   │InterfaceAttributeValue   │
   │random(), srandom(),    │ Thread safety │ MT-Safe │
   │initstate(), setstate() │               │         │


   POSIX.1-2001, POSIX.1-2008, 4.3BSD.


   Current "optimal" values for the size of the state array n are  8,  32,
   64,  128,  and  256  bytes;  other  amounts will be rounded down to the
   nearest known amount.  Using less than 8 bytes will cause an error.

   This function should not be used in cases where  multiple  threads  use
   random()  and the behavior should be reproducible.  Use random_r(3) for
   that purpose.

   Random-number generation is a complex topic.  Numerical Recipes  in  C:
   The  Art  of Scientific Computing (William H. Press, Brian P. Flannery,
   Saul  A.  Teukolsky,  William  T.  Vetterling;  New   York:   Cambridge
   University  Press,  2007, 3rd ed.)  provides an excellent discussion of
   practical  random-number  generation  issues  in  Chapter   7   (Random

   For  a  more  theoretical  discussion  which also covers many practical
   issues in depth, see Chapter 3 (Random Numbers) in  Donald  E.  Knuth's
   The  Art  of Computer Programming, volume 2 (Seminumerical Algorithms),
   2nd ed.; Reading,  Massachusetts:  Addison-Wesley  Publishing  Company,


   According  to  POSIX,  initstate() should return NULL on error.  In the
   glibc implementation, errno is (as specified) set  on  error,  but  the
   function does not return NULL.


   getrandom(2), drand48(3), rand(3), random_r(3), srand(3)


   This  page  is  part of release 4.09 of the Linux man-pages project.  A
   description of the project, information about reporting bugs,  and  the
   latest     version     of     this    page,    can    be    found    at

More Linux Commands

Tcl_Write(3) - buffered I/O facilities using channels.......
The Tcl channel mechanism provides a device-independent and platform-independent mechanism for performing buffered input and output operations on a variety of f

XF86Misc(3) - Extension library for the XFree86-Misc X exten
These functions provide an interface to the XFree86-Misc extension which allows various server settings to be queried and changed dynamically. Applications that

Tcl_SourceRCFile(3) - source the Tcl rc file (Man Page).....
Tcl_SourceRCFile is used to source the Tcl rc file at startup. It is typically invoked by Tcl_Main or Tk_Main. The name of the file sourced is obtained from the

unregister_all_config_handlers(3) - netsnmp_config_api funct
The functions are a fairly extensible system of parsing various configuration files at the run time of an application. The configuration file flow is broken int

persistent-keyring(7) Per-user persistent keyring (ManPage)
The persistent keyring is a keyring used to anchor keys on behalf of a user. Each UID the kernel deals with has its own persistent keyring that is shared betwee

snmp_pdu_create(3) - netsnmp_pdu_api functions (Man Page)...
These functions deal with SNMP request structures. snmp_pdu_create snmp_clone_pdu snmp_fix_pdu snmp_free_pdu SEE ALSO varbind_api(3) session_api(3) snmp_pdu_cre

_tracecchar_t2(3ncurses) - curses debugging routines........
The trace routines are used for debugging the ncurses libraries, as well as applications which use the ncurses libraries. These functions are normally available

gnutls_privkey_import_pkcs11(3) - API function (Man Page)...
This function will import the given private key to the abstract gnutls_privkey_t structure. The gnutls_pkcs11_privkey_t object must not be deallocated during th

glColor3us(3gl) - set the current color - Linux manual page
The GL stores both a current single-valued color index and a current four-valued RGBA color. glColor sets a new four-valued RGBA color. glColor has two major va

DPMSSetTimeouts(3) - permits applications to set the timeout
The DPMSSetTimeouts function permits applications to set the timeout values used by the X server for Display Power Management Signaling (DPMS) timings. The valu

terminal-colors.d(5) Configure output colorization for vario
Files in this directory determine the default behavior for utilities when coloring output. The name is a utility name. The name is optional and when none is spe

Tcl_AddErrorInfo(3) - retrieve or record information about e
The Tcl_SetReturnOptions and Tcl_GetReturnOptions routines expose the &amp;#9474; same capabilities as the return and catch commands, respectively, in &amp;#9474; the f

We can't live, work or learn in freedom unless the software we use is free.