xref: /dflybsd-src/lib/libc/gen/rand48.3 (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino.\" Copyright (c) 1993 Martin Birgmeier
286d7f5d3SJohn Marino.\" All rights reserved.
386d7f5d3SJohn Marino.\"
486d7f5d3SJohn Marino.\" You may redistribute unmodified or modified versions of this source
586d7f5d3SJohn Marino.\" code provided that the above copyright notice and this and the
686d7f5d3SJohn Marino.\" following conditions are retained.
786d7f5d3SJohn Marino.\"
886d7f5d3SJohn Marino.\" This software is provided ``as is'', and comes with no warranties
986d7f5d3SJohn Marino.\" of any kind. I shall in no event be liable for anything that happens
1086d7f5d3SJohn Marino.\" to anyone/anything when using this software.
1186d7f5d3SJohn Marino.\"
1286d7f5d3SJohn Marino.\"     @(#)rand48.3 V1.0 MB 8 Oct 1993
1386d7f5d3SJohn Marino.\" $FreeBSD: src/lib/libc/gen/rand48.3,v 1.8.2.6 2003/03/15 15:11:05 trhodes Exp $
1486d7f5d3SJohn Marino.\" $DragonFly: src/lib/libc/gen/rand48.3,v 1.4 2008/04/20 19:21:42 swildner Exp $
1586d7f5d3SJohn Marino.\"
1686d7f5d3SJohn Marino.Dd October 8, 1993
1786d7f5d3SJohn Marino.Dt RAND48 3
1886d7f5d3SJohn Marino.Os
1986d7f5d3SJohn Marino.Sh NAME
2086d7f5d3SJohn Marino.Nm drand48 ,
2186d7f5d3SJohn Marino.Nm erand48 ,
2286d7f5d3SJohn Marino.Nm lrand48 ,
2386d7f5d3SJohn Marino.Nm nrand48 ,
2486d7f5d3SJohn Marino.Nm mrand48 ,
2586d7f5d3SJohn Marino.Nm jrand48 ,
2686d7f5d3SJohn Marino.Nm srand48 ,
2786d7f5d3SJohn Marino.Nm seed48 ,
2886d7f5d3SJohn Marino.Nm lcong48
2986d7f5d3SJohn Marino.Nd pseudo random number generators and initialization routines
3086d7f5d3SJohn Marino.Sh LIBRARY
3186d7f5d3SJohn Marino.Lb libc
3286d7f5d3SJohn Marino.Sh SYNOPSIS
3386d7f5d3SJohn Marino.In stdlib.h
3486d7f5d3SJohn Marino.Ft double
3586d7f5d3SJohn Marino.Fn drand48 void
3686d7f5d3SJohn Marino.Ft double
3786d7f5d3SJohn Marino.Fn erand48 "unsigned short xseed[3]"
3886d7f5d3SJohn Marino.Ft long
3986d7f5d3SJohn Marino.Fn lrand48 void
4086d7f5d3SJohn Marino.Ft long
4186d7f5d3SJohn Marino.Fn nrand48 "unsigned short xseed[3]"
4286d7f5d3SJohn Marino.Ft long
4386d7f5d3SJohn Marino.Fn mrand48 void
4486d7f5d3SJohn Marino.Ft long
4586d7f5d3SJohn Marino.Fn jrand48 "unsigned short xseed[3]"
4686d7f5d3SJohn Marino.Ft void
4786d7f5d3SJohn Marino.Fn srand48 "long seed"
4886d7f5d3SJohn Marino.Ft "unsigned short *"
4986d7f5d3SJohn Marino.Fn seed48 "unsigned short xseed[3]"
5086d7f5d3SJohn Marino.Ft void
5186d7f5d3SJohn Marino.Fn lcong48 "unsigned short p[7]"
5286d7f5d3SJohn Marino.Sh DESCRIPTION
5386d7f5d3SJohn MarinoThe
5486d7f5d3SJohn Marino.Nm rand48
5586d7f5d3SJohn Marinofamily of functions generates pseudo-random numbers using a linear
5686d7f5d3SJohn Marinocongruential algorithm working on integers 48 bits in size.
5786d7f5d3SJohn MarinoThe
5886d7f5d3SJohn Marinoparticular formula employed is
5986d7f5d3SJohn Marinor(n+1) = (a * r(n) + c) mod m
6086d7f5d3SJohn Marinowhere the default values are
6186d7f5d3SJohn Marinofor the multiplicand a = 0xfdeece66d = 25214903917 and
6286d7f5d3SJohn Marinothe addend c = 0xb = 11.
6386d7f5d3SJohn MarinoThe modulo is always fixed at m = 2 ** 48.
6486d7f5d3SJohn Marinor(n) is called the seed of the random number generator.
6586d7f5d3SJohn Marino.Pp
6686d7f5d3SJohn MarinoFor all the six generator routines described next, the first
6786d7f5d3SJohn Marinocomputational step is to perform a single iteration of the algorithm.
6886d7f5d3SJohn Marino.Pp
6986d7f5d3SJohn MarinoThe
7086d7f5d3SJohn Marino.Fn drand48
7186d7f5d3SJohn Marinoand
7286d7f5d3SJohn Marino.Fn erand48
7386d7f5d3SJohn Marinofunctions
7486d7f5d3SJohn Marinoreturn values of type double.
7586d7f5d3SJohn MarinoThe full 48 bits of r(n+1) are
7686d7f5d3SJohn Marinoloaded into the mantissa of the returned value, with the exponent set
7786d7f5d3SJohn Marinosuch that the values produced lie in the interval [0.0, 1.0).
7886d7f5d3SJohn Marino.Pp
7986d7f5d3SJohn MarinoThe
8086d7f5d3SJohn Marino.Fn lrand48
8186d7f5d3SJohn Marinoand
8286d7f5d3SJohn Marino.Fn nrand48
8386d7f5d3SJohn Marinofunctions
8486d7f5d3SJohn Marinoreturn values of type long in the range
8586d7f5d3SJohn Marino[0, 2**31-1]. The high-order (31) bits of
8686d7f5d3SJohn Marinor(n+1) are loaded into the lower bits of the returned value, with
8786d7f5d3SJohn Marinothe topmost (sign) bit set to zero.
8886d7f5d3SJohn Marino.Pp
8986d7f5d3SJohn MarinoThe
9086d7f5d3SJohn Marino.Fn mrand48
9186d7f5d3SJohn Marinoand
9286d7f5d3SJohn Marino.Fn jrand48
9386d7f5d3SJohn Marinofunctions
9486d7f5d3SJohn Marinoreturn values of type long in the range
9586d7f5d3SJohn Marino[-2**31, 2**31-1]. The high-order (32) bits of
9686d7f5d3SJohn Marinor(n+1) are loaded into the returned value.
9786d7f5d3SJohn Marino.Pp
9886d7f5d3SJohn MarinoThe
9986d7f5d3SJohn Marino.Fn drand48 ,
10086d7f5d3SJohn Marino.Fn lrand48 ,
10186d7f5d3SJohn Marinoand
10286d7f5d3SJohn Marino.Fn mrand48
10386d7f5d3SJohn Marinofunctions
10486d7f5d3SJohn Marinouse an internal buffer to store r(n). For these functions
10586d7f5d3SJohn Marinothe initial value of r(0) = 0x1234abcd330e = 20017429951246.
10686d7f5d3SJohn Marino.Pp
10786d7f5d3SJohn MarinoOn the other hand,
10886d7f5d3SJohn Marino.Fn erand48 ,
10986d7f5d3SJohn Marino.Fn nrand48 ,
11086d7f5d3SJohn Marinoand
11186d7f5d3SJohn Marino.Fn jrand48
11286d7f5d3SJohn Marinouse a user-supplied buffer to store the seed r(n),
11386d7f5d3SJohn Marinowhich consists of an array of 3 shorts, where the zeroth member
11486d7f5d3SJohn Marinoholds the least significant bits.
11586d7f5d3SJohn Marino.Pp
11686d7f5d3SJohn MarinoAll functions share the same multiplicand and addend.
11786d7f5d3SJohn Marino.Pp
11886d7f5d3SJohn MarinoThe
11986d7f5d3SJohn Marino.Fn srand48
12086d7f5d3SJohn Marinofunction
12186d7f5d3SJohn Marinois used to initialize the internal buffer r(n) of
12286d7f5d3SJohn Marino.Fn drand48 ,
12386d7f5d3SJohn Marino.Fn lrand48 ,
12486d7f5d3SJohn Marinoand
12586d7f5d3SJohn Marino.Fn mrand48
12686d7f5d3SJohn Marinosuch that the 32 bits of the seed value are copied into the upper 32 bits
12786d7f5d3SJohn Marinoof r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
12886d7f5d3SJohn MarinoAdditionally, the constant multiplicand and addend of the algorithm are
12986d7f5d3SJohn Marinoreset to the default values given above.
13086d7f5d3SJohn Marino.Pp
13186d7f5d3SJohn MarinoThe
13286d7f5d3SJohn Marino.Fn seed48
13386d7f5d3SJohn Marinofunction
13486d7f5d3SJohn Marinoalso initializes the internal buffer r(n) of
13586d7f5d3SJohn Marino.Fn drand48 ,
13686d7f5d3SJohn Marino.Fn lrand48 ,
13786d7f5d3SJohn Marinoand
13886d7f5d3SJohn Marino.Fn mrand48 ,
13986d7f5d3SJohn Marinobut here all 48 bits of the seed can be specified in an array of 3 shorts,
14086d7f5d3SJohn Marinowhere the zeroth member specifies the lowest bits.
14186d7f5d3SJohn MarinoAgain,
14286d7f5d3SJohn Marinothe constant multiplicand and addend of the algorithm are
14386d7f5d3SJohn Marinoreset to the default values given above.
14486d7f5d3SJohn MarinoThe
14586d7f5d3SJohn Marino.Fn seed48
14686d7f5d3SJohn Marinofunction
14786d7f5d3SJohn Marinoreturns a pointer to an array of 3 shorts which contains the old seed.
14886d7f5d3SJohn MarinoThis array is statically allocated, thus its contents are lost after
14986d7f5d3SJohn Marinoeach new call to
15086d7f5d3SJohn Marino.Fn seed48 .
15186d7f5d3SJohn Marino.Pp
15286d7f5d3SJohn MarinoFinally,
15386d7f5d3SJohn Marino.Fn lcong48
15486d7f5d3SJohn Marinoallows full control over the multiplicand and addend used in
15586d7f5d3SJohn Marino.Fn drand48 ,
15686d7f5d3SJohn Marino.Fn erand48 ,
15786d7f5d3SJohn Marino.Fn lrand48 ,
15886d7f5d3SJohn Marino.Fn nrand48 ,
15986d7f5d3SJohn Marino.Fn mrand48 ,
16086d7f5d3SJohn Marinoand
16186d7f5d3SJohn Marino.Fn jrand48 ,
16286d7f5d3SJohn Marinoand the seed used in
16386d7f5d3SJohn Marino.Fn drand48 ,
16486d7f5d3SJohn Marino.Fn lrand48 ,
16586d7f5d3SJohn Marinoand
16686d7f5d3SJohn Marino.Fn mrand48 .
16786d7f5d3SJohn MarinoAn array of 7 shorts is passed as argument; the first three shorts are
16886d7f5d3SJohn Marinoused to initialize the seed; the second three are used to initialize the
16986d7f5d3SJohn Marinomultiplicand; and the last short is used to initialize the addend.
17086d7f5d3SJohn MarinoIt is thus not possible to use values greater than 0xffff as the addend.
17186d7f5d3SJohn Marino.Pp
17286d7f5d3SJohn MarinoNote that all three methods of seeding the random number generator
17386d7f5d3SJohn Marinoalways also set the multiplicand and addend for any of the six
17486d7f5d3SJohn Marinogenerator calls.
17586d7f5d3SJohn Marino.Pp
17686d7f5d3SJohn MarinoFor a more powerful random number generator, see
17786d7f5d3SJohn Marino.Xr random 3 .
17886d7f5d3SJohn Marino.Sh SEE ALSO
17986d7f5d3SJohn Marino.Xr rand 3 ,
18086d7f5d3SJohn Marino.Xr random 3
18186d7f5d3SJohn Marino.Sh AUTHORS
18286d7f5d3SJohn Marino.An Martin Birgmeier
183