|
@ -29,7 +29,7 @@ |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" SUCH DAMAGE. |
|
|
.\" SUCH DAMAGE. |
|
|
.\" |
|
|
.\" |
|
|
.\" $OpenBSD: random.3,v 1.11 2000/04/03 23:23:48 millert Exp $ |
|
|
|
|
|
|
|
|
.\" $OpenBSD: random.3,v 1.12 2000/04/20 13:50:02 aaron Exp $ |
|
|
.\" |
|
|
.\" |
|
|
.Dd April 19, 1991 |
|
|
.Dd April 19, 1991 |
|
|
.Dt RANDOM 3 |
|
|
.Dt RANDOM 3 |
|
@ -56,23 +56,27 @@ |
|
|
.Sh DESCRIPTION |
|
|
.Sh DESCRIPTION |
|
|
The |
|
|
The |
|
|
.Fn random |
|
|
.Fn random |
|
|
function |
|
|
|
|
|
uses a non-linear additive feedback random number generator employing a |
|
|
|
|
|
default table of size 31 long integers to return successive pseudo-random |
|
|
|
|
|
|
|
|
function uses a non-linear additive feedback random number generator employing |
|
|
|
|
|
a default table of size 31 long integers to return successive pseudo-random |
|
|
numbers in the range from 0 to (2**31)\-1. |
|
|
numbers in the range from 0 to (2**31)\-1. |
|
|
The period of this random number generator is very large, approximately |
|
|
The period of this random number generator is very large, approximately |
|
|
16*((2**31)\-1. |
|
|
16*((2**31)\-1. |
|
|
.Pp |
|
|
.Pp |
|
|
The |
|
|
The |
|
|
.Fn random Ns / Fn srandom |
|
|
|
|
|
have (almost) the same calling sequence and initialization properties as |
|
|
|
|
|
|
|
|
.Fn random |
|
|
|
|
|
and |
|
|
|
|
|
.Fn srandom |
|
|
|
|
|
functions have (almost) the same calling sequence and initialization |
|
|
|
|
|
properties as |
|
|
.Xr rand 3 Ns / Xr srand 3 . |
|
|
.Xr rand 3 Ns / Xr srand 3 . |
|
|
The difference is that |
|
|
The difference is that |
|
|
.Xr rand |
|
|
.Xr rand |
|
|
produces a much less random sequence \(em in fact, the low dozen bits |
|
|
produces a much less random sequence \(em in fact, the low dozen bits |
|
|
generated by rand go through a cyclic pattern. All the bits generated by |
|
|
|
|
|
|
|
|
generated by rand go through a cyclic pattern. |
|
|
|
|
|
All the bits generated by |
|
|
.Fn random |
|
|
.Fn random |
|
|
are usable. For example, |
|
|
|
|
|
|
|
|
are usable. |
|
|
|
|
|
For example, |
|
|
.Sq Li random()&01 |
|
|
.Sq Li random()&01 |
|
|
will produce a random binary |
|
|
will produce a random binary |
|
|
value. |
|
|
value. |
|
@ -81,9 +85,10 @@ Unlike |
|
|
.Xr srand , |
|
|
.Xr srand , |
|
|
.Fn srandom |
|
|
.Fn srandom |
|
|
does not return the old seed; the reason for this is that the amount of |
|
|
does not return the old seed; the reason for this is that the amount of |
|
|
state information used is much more than a single word. (Two other |
|
|
|
|
|
routines are provided to deal with restarting/changing random |
|
|
|
|
|
number generators). Like |
|
|
|
|
|
|
|
|
state information used is much more than a single word. |
|
|
|
|
|
(Two other routines are provided to deal with restarting/changing random |
|
|
|
|
|
number generators). |
|
|
|
|
|
Like |
|
|
.Xr rand 3 , |
|
|
.Xr rand 3 , |
|
|
however, |
|
|
however, |
|
|
.Fn random |
|
|
.Fn random |
|
@ -110,28 +115,28 @@ a fixed seed. |
|
|
The |
|
|
The |
|
|
.Fn initstate |
|
|
.Fn initstate |
|
|
routine allows a state array, passed in as an argument, to be initialized |
|
|
routine allows a state array, passed in as an argument, to be initialized |
|
|
for future use. The size of the state array (in bytes) is used by |
|
|
|
|
|
|
|
|
for future use. |
|
|
|
|
|
The size of the state array (in bytes) is used by |
|
|
.Fn initstate |
|
|
.Fn initstate |
|
|
to decide how sophisticated a random number generator it should use \(em the |
|
|
to decide how sophisticated a random number generator it should use \(em the |
|
|
more state, the better the random numbers will be. |
|
|
more state, the better the random numbers will be. |
|
|
(Current "optimal" values for the amount of state information are |
|
|
(Current "optimal" values for the amount of state information are |
|
|
8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to |
|
|
8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to |
|
|
the nearest known amount. Using less than 8 bytes will cause an error.) |
|
|
|
|
|
|
|
|
the nearest known amount. |
|
|
|
|
|
Using less than 8 bytes will cause an error.) |
|
|
The seed for the initialization (which specifies a starting point for |
|
|
The seed for the initialization (which specifies a starting point for |
|
|
the random number sequence, and provides for restarting at the same |
|
|
the random number sequence, and provides for restarting at the same |
|
|
point) is also an argument. |
|
|
point) is also an argument. |
|
|
The |
|
|
The |
|
|
.Fn initstate |
|
|
.Fn initstate |
|
|
function |
|
|
|
|
|
returns a pointer to the previous state information array. |
|
|
|
|
|
|
|
|
function returns a pointer to the previous state information array. |
|
|
.Pp |
|
|
.Pp |
|
|
Once a state has been initialized, the |
|
|
Once a state has been initialized, the |
|
|
.Fn setstate |
|
|
.Fn setstate |
|
|
routine provides for rapid switching between states. |
|
|
routine provides for rapid switching between states. |
|
|
The |
|
|
The |
|
|
.Fn setstate |
|
|
.Fn setstate |
|
|
function |
|
|
|
|
|
returns a pointer to the previous state array; its |
|
|
|
|
|
|
|
|
function returns a pointer to the previous state array; its |
|
|
argument state array is used for further random number generation |
|
|
argument state array is used for further random number generation |
|
|
until the next call to |
|
|
until the next call to |
|
|
.Fn initstate |
|
|
.Fn initstate |
|
|