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