home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bsdlib8f.zip / BSDDOC.ZIP / EMX / BSD / MAN / RANDOM.MAN < prev   
Text File  |  1992-08-28  |  4KB  |  86 lines

  1. NAME
  2.      random, srandom, initstate, setstate - better random number generator;
  3.      routines for changing generators
  4.  
  5. SYNOPSIS
  6.      #include <stdlib>
  7.  
  8.      long
  9.      random(void)
  10.  
  11.      void
  12.      srandom(unsigned seed)
  13.  
  14.      char *
  15.      initstate(unsigned seed, char *state, int n)
  16.  
  17.      char *
  18.      setstate(char *state)
  19.  
  20. DESCRIPTION
  21.      The random() function uses a non-linear additive feedback random number
  22.      generator employing a default table of size 31 long integers to return
  23.      successive pseudo-random numbers in the range from 0 to (2**31)-1.  The
  24.      period of this random number generator is very large, approximately
  25.      16*((2**31)-1).
  26.  
  27.      The random()/ srandom() have (almost) the same calling sequence and ini-
  28.      tialization properties as rand(3)/ srand(3).   The difference is that
  29.      rand produces a much less random sequence -- in fact, the low dozen bits
  30.      generated by rand go through a cyclic pattern.  All the bits generated by
  31.      random() are usable.  For example, `random()&01' will produce a random
  32.      binary value.
  33.  
  34.      Unlike srand,  srandom() does not return the old seed; the reason for
  35.      this is that the amount of state information used is much more than a
  36.      single word.  (Two other routines are provided to deal with restart-
  37.      ing/changing random number generators).  Like rand(3),  however, random()
  38.      will by default produce a sequence of numbers that can be duplicated by
  39.      calling srandom() with `1' as the seed.
  40.  
  41.      The initstate() routine allows a state array, passed in as an argument,
  42.      to be initialized for future use.  The size of the state array (in bytes)
  43.      is used by initstate() to decide how sophisticated a random number gener-
  44.      ator it should use -- the more state, the better the random numbers will
  45.      be.  (Current "optimal" values for the amount of state information are 8,
  46.      32, 64, 128, and 256 bytes; other amounts will be rounded down to the
  47.      nearest known amount.  Using less than 8 bytes will cause an error.)  The
  48.      seed for the initialization (which specifies a starting point for the
  49.      random number sequence, and provides for restarting at the same point) is
  50.      also an argument.  The initstate() function returns a pointer to the pre-
  51.      vious state information array.
  52.  
  53.      Once a state has been initialized, the setstate() routine provides for
  54.      rapid switching between states.  The setstate() function returns a point-
  55.      er to the previous state array; its argument state array is used for fur-
  56.      ther random number generation until the next call to initstate() or
  57.      setstate().
  58.  
  59.      Once a state array has been initialized, it may be restarted at a differ-
  60.      ent point either by calling initstate() (with the desired seed, the state
  61.      array, and its size) or by calling both setstate() (with the state array)
  62.      and srandom() (with the desired seed).  The advantage of calling both
  63.      setstate() and srandom() is that the size of the state array does not
  64.      have to be remembered after it is initialized.
  65.  
  66.      With 256 bytes of state information, the period of the random number gen-
  67.      erator is greater than 2**69 which should be sufficient for most purpos-
  68.      es.
  69.  
  70. AUTHOR
  71.      Earl T. Cohen
  72.  
  73. DIAGNOSTICS
  74.      If initstate() is called with less than 8 bytes of state information, or
  75.      if setstate() detects that the state information has been garbled, error
  76.      messages are printed on the standard error output.
  77.  
  78. SEE ALSO
  79.      rand(3)
  80.  
  81. HISTORY
  82.      These functions appeared in 4.2BSD.
  83.  
  84. BUGS
  85.      About 2/3 the speed of rand(3).
  86.