LCRANS(3M) — MATHEMATICAL LIBRARY
NAME
lcrans − linear congruential pseudo-random number generators
SYNOPSIS
#include <math.h>
#define LCRAN_MODULUS 2147483647
#define LCRAN_MULTIPLIER 16807
int i_lcran_()
double d_lcran_()
FLOATFUNCTIONTYPE r_lcran_()
void u_lcrans_(x,n,l,u)
unsigned ∗x, ∗l, ∗u;
int ∗n;
void i_lcrans_(x,n,l,u)
int ∗x, ∗l, ∗u;
int ∗n;
void d_lcrans_(x,n,l,u)
double ∗x, ∗l, ∗u;
int ∗n;
void r_lcrans_(x,n,l,u)
float ∗x, ∗l, ∗u;
int ∗n;
void i_get_lcrans_(x)
int ∗x ;
void i_set_lcrans_(x)
int ∗x ;
void i_init_lcrans()
DESCRIPTION
These functions provide uniform random variates, of types integer, single-precision floating-point, or double-precision floating-point.
Linear congruential variates are generated one at a time by ..._lcran_() using the recurrence
lcran_last = (16807 ∗ lcran_last) % 2147483647
i_lcran_() returns lcran_last, while r_lcran_() and d_lcran_() return lcran_last / 2147483647, so that the variates are contained in the following intervals:
| type | lower bound | upper bound | ||
| name | value | name | value | |
| integer | I_LCRAN_LB | 1 | I_LCRAN_UB | 2147483646 |
| float | R_LCRAN_LB | 4.656612873077392578E-10 | R_LCRAN_UB | 1 |
| double | D_LCRAN_LB | 4.656612875245796923E-10 | D_LCRAN_UB | 0.9999999995343387127 |
lcran_last should satisfy 1 <= lcran_last <= 2147483646, but for efficiency ..._lcran_() does not check that.
Linear congruential variates are generated ∗n at a time by ..._lcrans_() using the recurrence
lcran_last = (lcran_multiplier ∗ lcran_last) % 2147483647 ;
return scale ∗ (lcran_last + offset) ;
where scale and offset are calculated from ∗l and ∗u so that the computed variates are uniform in [∗l,∗u]. Thus changing lcran_multiplier changes the variates produced by ..._lcrans_(), but for efficiency ..._lcran_() uses the fixed LCRAN_MULTIPLIER, 16807. The fixed modulus LCRAN_MODULUS, 2147483647, is intentionally not a power of two to avoid the defects in rand(3C). On entry, ..._lcrans_() does check that 1 <= lcran_last <= 2147483646 and modifies lcran_last if necessary.
The state of the linear congruential generator (an array of two ints containing lcran_last and lcran_multiplier) may be obtained with i_get_lcrans_() and set with i_set_lcrans_(). The initial state may be restored with i_init_lcrans_().
EXAMPLES
to generate 1000 double-precision random variates in (0,1)
double x[1000] ; int n = 1000 ;
double lb = D_LCRAN_LB, ub = D_LCRAN_UB ;
for ( ; n > 0 ; n--) ∗x+ = d_lcran_();/∗ or... ∗/
d_lcrans_(x, &n, &lb, &ub) ;/∗ same output, but more efficient ∗/
to generate 1000 integer random variates in [-10,+10]
int x[1000] ; int n = 1000, lb = −10, ub = −10 ;
i_lcrans_(x, &n, &lb, &ub) ;
FILES
/usr/include/math.h
function declarations
/usr/lib/libm.a function archive
SEE ALSO
addrans(3M), drand48(3), rand(3C), rand(3V), random(3), shufrans(3M).
Knuth, Seminumerical Algorithms, 1981, Addison-Wesley.
Park and Miller, Random Number Generators: Good Ones are Hard to Find, Communications of the ACM, October 1988.
NOTES
The functions in addrans(3M) are generally more efficient but their theory is not as refined as those in lcrans(3M). Either for efficiency or for theory, the addrans(3M) functions are recommended for normal use over those in drand48(3), lcrans(3M), rand(3C), rand(3V), and random(3).
Sun Release 4.0 — Last change: 20 May 1989