Blob
1 .TH RAND 32 .SH NAME3 rand, lrand, frand, nrand, lnrand, srand, truerand, ntruerand, fastrand, nfastrand \- random number generator4 .SH SYNOPSIS5 .B #include <u.h>6 .br7 .B #include <libc.h>8 .PP9 .ta \w'\fLdouble 'u10 .B11 int rand(void)12 .PP13 .B14 long lrand(void)15 .PP16 .B17 double frand(void)18 .PP19 .B20 int nrand(int val)21 .PP22 .B23 long lnrand(long val)24 .PP25 .B26 void srand(long seed)27 .PP28 .B29 ulong truerand(void)30 .PP31 .B32 ulong ntruerand(ulong val)33 .sp34 .B #include <mp.h>35 .br36 .B #include <libsec.h>37 .PP38 .B39 void genrandom(uchar *buf, int nbytes)40 .PP41 .B42 void prng(uchar *buf, int nbytes)43 .PP44 .B45 ulong fastrand(void)46 .PP47 .B48 ulong nfastrand(ulong val)49 .SH DESCRIPTION50 .I Rand51 returns a uniform pseudo-random52 number53 .IR x ,54 .RI 0≤ x <2\u\s715\s10\d.55 .PP56 .I Lrand57 returns a uniform58 .B long59 .IR x ,60 .RI 0≤ x <2\u\s731\s10\d.61 .PP62 .I Frand63 returns a uniform64 .B double65 .IR x ,66 .RI 0.0≤ x <1.0,67 This function calls68 .I lrand69 twice to generate a number with as many as 62 significant bits of mantissa.70 .PP71 .I Nrand72 returns a uniform integer73 .IR x ,74 .RI 0≤ x < val.75 .I Lnrand76 is the same, but returns a77 .BR long .78 .PP79 The algorithm is additive feedback with:80 .IP81 x[n] = (x[n\(mi273] + x[n\(mi607]) mod82 .if t 2\u\s731\s0\d83 .if n 2^3184 .LP85 giving a period of86 .if t 2\u\s730\s10\d \(mu (2\u\s7607\s10\d \- 1).87 .if n 2^30 × (2^607 - 1).88 .PP89 The generators are initialized by calling90 .I srand91 with whatever you like as argument.92 To get a different starting value each time,93 .IP94 .L95 srand(time(0))96 .LP97 will work as long as it is not called more often98 than once per second.99 Calling100 .IP101 .L102 srand(1)103 .LP104 will initialize the generators to their105 starting state.106 .PP107 .I Truerand108 returns a random unsigned long read from109 .BR /dev/random .110 Due to the nature of111 .BR /dev/random ,112 truerand can only return a few hundred bits a113 second.114 .PP115 .I Ntruerand116 returns a uniform random integer117 .IR x ,118 .RI 0≤ x < val ≤ 2\u\s732\s10\d-1.119 .PP120 .I Genrandom121 fills a buffer with bytes from the X9.17 pseudo-random122 number generator. The X9.17 generator is seeded by 24123 truly random bytes read from124 .BR /dev/random .125 .PP126 .I Prng127 uses the native128 .IR rand (3)129 pseudo-random number generator to fill the buffer. Used with130 .IR srand ,131 this function can produce a reproducible stream of pseudo random132 numbers useful in testing.133 .PP134 Both135 .I genrandom136 and137 .I prng138 may be passed to139 .I mprand140 (see141 .IR mp (3)).142 .PP143 .I Fastrand144 uses145 .I genrandom146 to return a uniform147 .B "unsigned long148 .IR x ,149 .RI 0≤ x < 2\u\s732\s10\d-1.150 .PP151 .I Nfastrand152 uses153 .I genrandom154 to return a uniform155 .B "unsigned long156 .IR x ,157 .RI 0≤ x < val ≤ 2\u\s732\s10\d-1.158 .SH SOURCE159 .B \*9/src/lib9160 .br161 .B \*9/src/libsec/port162 .SH "SEE ALSO163 .\" .IR cons (3),164 .IR mp (3)165 .SH BUGS166 .I Truerand167 and168 .I ntruerand169 maintain a static file descriptor.170 .PP171 To avoid name conflicts with the underlying system,172 .IR rand ,173 .IR lrand ,174 .IR frand ,175 .IR nrand ,176 .IR lnrand ,177 and178 .I srand179 are preprocessor macros defined as180 .IR p9rand ,181 .IR p9lrand ,182 and so on;183 see184 .IR intro (3).185 .ie \n(HT .ds HT "186 .el .ds HT " (see HTML-formatted man page for link)187 .PP188 Some versions of SunOS do not ship with189 .B /dev/random190 installed.191 You may have to download a192 .HR "http://sunsolve.sun.com/search/document.do?assetkey=1-25-27606-1" "patch from Sun\*(HT"193 to add it to your system.