Home | History | Annotate | Line # | Download | only in stdlib
rand48.3 revision 1.1.2.1
      1 \" Copyright (c) 1993 Martin Birgmeier
      2 .\" All rights reserved.
      3 .\"
      4 .\" You may redistribute unmodified or modified versions of this source
      5 .\" code provided that the above copyright notice and this and the
      6 .\" following conditions are retained.
      7 .\"
      8 .\" This software is provided ``as is'', and comes with no warranties
      9 .\" of any kind. I shall in no event be liable for anything that happens
     10 .\" to anyone/anything when using this software.
     11 .\"
     12 .\"     $Id: rand48.3,v 1.1.2.1 1994/10/06 04:36:12 mycroft Exp $
     13 .\"
     14 .Dd October 8, 1993
     15 .Dt RAND48 3
     16 .Os
     17 .Sh NAME
     18 .Nm drand48 ,
     19 .Nm erand48 ,
     20 .Nm lrand48 ,
     21 .Nm nrand48 ,
     22 .Nm mrand48 ,
     23 .Nm jrand48 ,
     24 .Nm srand48 ,
     25 .Nm seed48 ,
     26 .Nm lcong48
     27 .Nd pseudo random number generators and initialization routines
     28 .Sh SYNOPSIS
     29 .Fd #include <stdlib.h>
     30 .Ft double 
     31 .Fn drand48 void
     32 .Ft double
     33 .Fn erand48 "unsigned short xseed[3]"
     34 .Ft long
     35 .Fn lrand48 void
     36 .Ft long
     37 .Fn nrand48 "unsigned short xseed[3]"
     38 .Ft long
     39 .Fn mrand48 void
     40 .Ft long
     41 .Fn jrand48 "unsigned short xseed[3]"
     42 .Ft void
     43 .Fn srand48 "long seed"
     44 .Ft "unsigned short *"
     45 .Fn seed48 "unsigned short xseed[3]"
     46 .Ft void
     47 .Fn lcong48 "unsigned short p[7]"
     48 .Sh DESCRIPTION
     49 The
     50 .Fn rand48
     51 family of functions generates pseudo-random numbers using a linear
     52 congruential algorithm working on integers 48 bits in size. The
     53 particular formula employed is
     54 r(n+1) = (a * r(n) + c) mod m
     55 where the default values are
     56 for the multiplicand a = 0xfdeece66d = 25214903917 and
     57 the addend c = 0xb = 11. The modulus is always fixed at m = 2 ** 48.
     58 r(n) is called the seed of the random number generator.
     59 .Pp
     60 For all the six generator routines described next, the first
     61 computational step is to perform a single iteration of the algorithm.
     62 .Pp
     63 .Fn drand48
     64 and
     65 .Fn erand48
     66 return values of type double. The full 48 bits of r(n+1) are
     67 loaded into the mantissa of the returned value, with the exponent set
     68 such that the values produced lie in the interval [0.0, 1.0).
     69 .Pp
     70 .Fn lrand48
     71 and
     72 .Fn nrand48
     73 return values of type long in the range
     74 [0, 2**31-1]. The high-order (31) bits of
     75 r(n+1) are loaded into the lower bits of the returned value, with
     76 the topmost (sign) bit set to zero.
     77 .Pp
     78 .Fn mrand48
     79 and
     80 .Fn jrand48
     81 return values of type long in the range
     82 [-2**31, 2**31-1]. The high-order (32) bits of
     83 r(n+1) are loaded into the returned value.
     84 .Pp
     85 .Fn drand48 ,
     86 .Fn lrand48 ,
     87 and
     88 .Fn mrand48
     89 use an internal buffer to store r(n). For these functions
     90 the initial value of r(0) = 0x1234abcd330e = 20017429951246.
     91 .Pp
     92 On the other hand,
     93 .Fn erand48 ,
     94 .Fn nrand48 ,
     95 and
     96 .Fn jrand48
     97 use a user-supplied buffer to store the seed r(n),
     98 which consists of an array of 3 shorts, where the zeroth member
     99 holds the least significant bits.
    100 .Pp
    101 All functions share the same multiplicand and addend.
    102 .Pp
    103 .Fn srand48
    104 is used to initialize the internal buffer r(n) of
    105 .Fn drand48 ,
    106 .Fn lrand48 ,
    107 and
    108 .Fn mrand48
    109 such that the 32 bits of the seed value are copied into the upper 32 bits
    110 of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
    111 Additionally, the constant multiplicand and addend of the algorithm are
    112 reset to the default values given above.
    113 .Pp
    114 .Fn seed48
    115 also initializes the internal buffer r(n) of
    116 .Fn drand48 ,
    117 .Fn lrand48 ,
    118 and
    119 .Fn mrand48 ,
    120 but here all 48 bits of the seed can be specified in an array of 3 shorts,
    121 where the zeroth member specifies the lowest bits. Again,
    122 the constant multiplicand and addend of the algorithm are
    123 reset to the default values given above.
    124 .Fn seed48
    125 returns a pointer to an array of 3 shorts which contains the old seed.
    126 This array is statically allocated, thus its contents are lost after
    127 each new call to
    128 .Fn seed48 .
    129 .Pp
    130 Finally,
    131 .Fn lcong48
    132 allows full control over the multiplicand and addend used in
    133 .Fn drand48 ,
    134 .Fn erand48 ,
    135 .Fn lrand48 ,
    136 .Fn nrand48 ,
    137 .Fn mrand48 ,
    138 and
    139 .Fn jrand48 ,
    140 and the seed used in
    141 .Fn drand48 ,
    142 .Fn lrand48 ,
    143 and
    144 .Fn mrand48 .
    145 An array of 7 shorts is passed as parameter; the first three shorts are
    146 used to initialize the seed; the second three are used to initialize the
    147 multiplicand; and the last short is used to initialize the addend.
    148 It is thus not possible to use values greater than 0xffff as the addend.
    149 .Pp
    150 Note that all three methods of seeding the random number generator
    151 always also set the multiplicand and addend for any of the six
    152 generator calls.
    153 .Pp
    154 For a more powerful random number generator, see
    155 .Xr random 3
    156 .Sh AUTHOR
    157 Martin Birgmeier
    158 .Sh SEE ALSO
    159 .Xr rand 3 ,
    160 .Xr random 3 .
    161