📄 random.3
字号:
.\" Copyright (c) 1983 Regents of the University of California..\" All rights reserved. The Berkeley software License Agreement.\" specifies the terms and conditions for redistribution..\".\" @(#)random.3 6.2 (Berkeley) 9/29/85.\".TH RANDOM 3 "September 29, 1985".UC 5.SH NAMErandom, srandom, initstate, setstate \- better random number generator; routines for changing generators.SH SYNOPSIS.nf.ft B#include <stdlib.h>long random(void)void srandom(unsigned \fIseed\fP)char *initstate(unsigned \fIseed\fP, char *\fIstate\fP, int \fIn\fP)char *setstate(char *\fIstate\fP).ft R.fi.SH DESCRIPTION.PP.B Randomuses a non-linear additive feedback random number generator employing adefault table of size 31 long integers to return successive pseudo-randomnumbers in the range from 0 to.if t 2\u\s731\s10\d\(mi1..if n (2**31)\(mi1.The period of this random number generator is very large, approximately.if t 16\(mu(2\u\s731\s10\d\(mi1)..if n 16*((2**31)\(mi1)..PP.B Random/srandomhave (almost) the same calling sequence and initialization properties as.B rand/srand.The difference is that.BR rand (3)produces a much less random sequence \(em in fact, the low dozen bitsgenerated by rand go through a cyclic pattern. All the bits generated by.B randomare usable. For example, ``random()&01'' will produce a random binaryvalue..PPUnlike.BR srand ,.B srandomdoes not return the old seed; the reason for this is that the amount ofstate information used is much more than a single word. (Two otherroutines are provided to deal with restarting/changing randomnumber generators). Like.BR rand (3),however,.B randomwill by default produce a sequence of numbers that can be duplicatedby calling.B srandomwith .B 1as the seed..PPThe.B initstateroutine allows a state array, passed in as an argument, to be initializedfor future use. The size of the state array (in bytes) is used by.B initstateto decide how sophisticated a random number generator it should use -- themore state, the better the random numbers will be.(Current "optimal" values for the amount of state information are8, 32, 64, 128, and 256 bytes; other amounts will be rounded down tothe nearest known amount. Using less than 8 bytes will cause an error).The seed for the initialization (which specifies a starting point forthe random number sequence, and provides for restarting at the samepoint) is also an argument..B Initstatereturns a pointer to the previous state information array..PPOnce a state has been initialized, the.B setstateroutine provides for rapid switching between states..B Setstatereturns a pointer to the previous state array; itsargument state array is used for further random number generationuntil the next call to.B initstateor.BR setstate ..PPOnce a state array has been initialized, it may be restarted at adifferent point either by calling.B initstate(with the desired seed, the state array, and its size) or by callingboth.B setstate(with the state array) and.B srandom(with the desired seed).The advantage of calling both.B setstateand.B srandomis that the size of the state array does not have to be remembered afterit is initialized..PPWith 256 bytes of state information, the period of the random numbergenerator is greater than.if t 2\u\s769\s10\d,.if n 2**69which should be sufficient for most purposes..SH AUTHOREarl T. Cohen.SH DIAGNOSTICS.PPIf.B initstateis called with less than 8 bytes of state information, or if.B setstatedetects that the state information has been garbled, errormessages are printed on the standard error output..SH "SEE ALSO".BR rand (3)..SH NOTES.B initstateand.B setstateare not declared in.IR <stdlib.h> ,programmers must provide their own declarations..SH BUGSAbout 2/3 the speed of.BR rand (3).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -