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