175 lines · 2.8 KB · Text
RawDownload
.TH RAND 2.SH NAMErand, lrand, frand, nrand, lnrand, srand, truerand, ntruerand, genrandom, prng, fastrand, nfastrand \- random number generators.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.PP.ta \w'\fLdouble 'u.Bint	rand(void).PP.Blong	lrand(void).PP.Bdouble	frand(void).PP.Bint	nrand(int val).PP.Blong	lnrand(long val).PP.Bvoid	srand(long seed).PP.Bulong	truerand(void).PP.Bulong	ntruerand(ulong val).sp.B #include <mp.h>.br.B #include <libsec.h>.PP.Bvoid	genrandom(uchar *buf, int nbytes).PP.Bvoid	prng(uchar *buf, int nbytes).PP.Bulong	fastrand(void).PP.Bulong	nfastrand(ulong val).SH DESCRIPTION.I Randreturns a uniform pseudo-randomnumber.IR x ,.RI 0≤ x <2\u\s715\s10\d..PP.I Lrandreturns a uniform.B long.IR x ,.RI 0≤ x <2\u\s731\s10\d..PP.I Frandreturns a uniform.B double.IR x ,.RI 0.0≤ x <1.0,This function calls.I lrandtwice to generate a number with as many as 62 significant bits of mantissa..PP.I Nrandreturns a uniform integer.IR x ,.RI 0≤ x < val..I Lnrandis the same, but returns a.BR long ..PPThe algorithm is additive feedback with:.IPx[n] = (x[n\(mi273] + x[n\(mi607]) mod.if t 2\u\s731\s0\d.if n 2^31.LPgiving a period of.if t 2\u\s730\s10\d \(mu (2\u\s7607\s10\d \- 1)..if n 2^30 × (2^607 - 1)..PPThe generators are initialized by calling.I srandwith whatever you like as argument.To get a different starting value each time,.IP.Lsrand(time(0)).LPwill work as long as it is not called more oftenthan once per second.Calling.IP.Lsrand(1).LPwill initialize the generators to theirstarting state..PP.I Truerandreturns a random unsigned long read from.BR /dev/random .Due to the nature of.BR /dev/random ,truerand can only return a few hundred bits asecond..PP.I Ntruerandreturns a uniform random integer.IR x ,.RI 0≤ x < val ≤ 2\u\s732\s10\d-1..PP.I Genrandomfills a buffer with bytes from the X9.17 pseudo-randomnumber generator.  The X9.17 generator is seeded by 24truly random bytes read from.BR /dev/random ..PP.I Prnguses the native.IR rand (2)pseudo-random number generator to fill the buffer.  Used with.IR srand ,this function can produce a reproducible stream of pseudo randomnumbers useful in testing..PPBoth.I genrandomand.I prngmay be passed to.I mprand(see.IR mp (2))..PP.I Fastranduses.I genrandomto return a uniform.B "unsigned long.IR x ,.RI 0≤ x < 2\u\s732\s10\d-1..PP.I Nfastranduses.I genrandomto return a uniform.B "unsigned long.IR x ,.RI 0≤ x < val ≤ 2\u\s732\s10\d-1..SH SOURCE.B /sys/src/libc/port/*rand.c.br.B /sys/src/libc/9sys/truerand.c.br.B /sys/src/libsec/port/genrandom.c.br.B /sys/src/libsec/port/prng.c.br.B /sys/src/libsec/port/*fastrand.c.SH "SEE ALSO.IR cons (3),.IR mp (2).SH BUGS.I Truerandand.I ntruerandmaintain a static file descriptor.