RAND(2)                                                   RAND(2)

          rand, lrand, frand, nrand, lnrand, srand, truerand,
          ntruerand, genrandom, prng, fastrand, nfastrand - random
          number generators

          #include <u.h>
          #include <libc.h>

          int    rand(void)

          long   lrand(void)

          double frand(void)

          int    nrand(int val)

          long   lnrand(long val)

          void   srand(long seed)

          ulong  truerand(void)

          ulong  ntruerand(ulong val)

          #include <libsec.h>

          void   genrandom(uchar *buf, int nbytes)

          void   prng(uchar *buf, int nbytes)

          ulong  fastrand(void)

          ulong  nfastrand(ulong val)

          Rand returns a uniform pseudo-random number x, 0≤ x <2^15.

          Lrand returns a uniform long x, 0≤ x <2^31.

          Frand returns a uniform double x, 0.0≤x<1.0, This function
          calls lrand twice to generate a number with as many as 62
          significant bits of mantissa.

          Nrand returns a uniform integer x, 0≤x<val.  Lnrand is the
          same, but returns a long.

          The algorithm is additive feedback with:

               x[n] = (x[n-273] + x[n-607]) mod 2^31

     Page 1                       Plan 9             (printed 6/16/24)

     RAND(2)                                                   RAND(2)

          giving a period of 2^30 × (2^607 - 1).

          The generators are initialized by calling srand with what-
          ever you like as argument.  To get a different starting
          value each time,


          will work as long as it is not called more often than once
          per second.  Calling


          will initialize the generators to their starting state.

          Truerand returns a random unsigned long read from

          Ntruerand returns a uniform random integer x, 0≤ x < val ≤

          Genrandom fills a buffer with bytes from the cryptographic
          pseudo-random number generator. The generator is automati-
          cally seeded by truerand.

          Prng uses the native rand(2) pseudo-random number generator
          to fill the buffer.  Used with srand, this function can pro-
          duce a reproducible stream of pseudo random numbers useful
          in testing.

          Both genrandom and prng may be passed to mprand (see mp(2)).

          Fastrand uses genrandom to return a uniform unsigned long x,
          0≤ x <2^32-1.

          Nfastrand uses genrandom to return a uniform unsigned long
          x, 0≤ x < val ≤ 2^32-1.


          cons(3), mp(2)

          Truerand and ntruerand maintain a static file descriptor.

     Page 2                       Plan 9             (printed 6/16/24)