| <head> |
| <title>rand(3) - Plan 9 from User Space</title> |
| <meta content="text/html; charset=utf-8" http-equiv=Content-Type> |
| </head> |
| <body bgcolor=#ffffff> |
| <table border=0 cellpadding=0 cellspacing=0 width=100%> |
| <tr height=10><td> |
| <tr><td width=20><td> |
| <tr><td width=20><td><b>RAND(3)</b><td align=right><b>RAND(3)</b> |
| <tr><td width=20><td colspan=2> |
| <br> |
| <p><font size=+1><b>NAME </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| rand, lrand, frand, nrand, lnrand, srand, truerand, ntruerand, |
| fastrand, nfastrand – random number generator<br> |
| |
| </table> |
| <p><font size=+1><b>SYNOPSIS </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <tt><font size=+1>#include <u.h><br> |
| #include <libc.h> |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>int rand(void) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>long lrand(void) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>double frand(void) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>int nrand(int val) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>long lnrand(long val) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>void srand(long seed) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>ulong truerand(void) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>ulong ntruerand(ulong val)<br> |
| #include <mp.h><br> |
| #include <libsec.h> |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>void genrandom(uchar *buf, int nbytes) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>void prng(uchar *buf, int nbytes) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>ulong fastrand(void) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>ulong nfastrand(ulong val)<br> |
| </font></tt> |
| </table> |
| <p><font size=+1><b>DESCRIPTION </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <i>Rand</i> returns a uniform pseudo-random number <i>x</i>, 0≤<i>x</i><215. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Lrand</i> returns a uniform <tt><font size=+1>long</font></tt> <i>x</i>, 0≤<i>x</i><231. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Frand</i> returns a uniform <tt><font size=+1>double</font></tt> <i>x</i>, 0.0≤<i>x</i><1.0, This function calls |
| <i>lrand</i> twice to generate a number with as many as 62 significant |
| bits of mantissa. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Nrand</i> returns a uniform integer <i>x</i>, 0≤<i>x</i><<i>val. Lnrand</i> is the same, |
| but returns a <tt><font size=+1>long</font></tt>. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| The algorithm is additive feedback with:<br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| x[n] = (x[n-273] + x[n-607]) mod 231 |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| |
| </table> |
| giving a period of 230 × (2607 – 1). |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| The generators are initialized by calling <i>srand</i> with whatever |
| you like as argument. To get a different starting value each time,<br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <tt><font size=+1>srand(time(0)) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| |
| </table> |
| will work as long as it is not called more often than once per |
| second. Calling<br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <tt><font size=+1>srand(1) |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| |
| </table> |
| will initialize the generators to their starting state. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Truerand</i> returns a random unsigned long read from <tt><font size=+1>/dev/random</font></tt>. |
| Due to the nature of <tt><font size=+1>/dev/random</font></tt>, truerand can only return a few |
| hundred bits a second. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Ntruerand</i> returns a uniform random integer <i>x</i>, 0≤<i>x</i><<i>val</i>≤<i>232-1. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </i> |
| <i>Genrandom</i> fills a buffer with bytes from the X9.17 pseudo-random |
| number generator. The X9.17 generator is seeded by 24 truly random |
| bytes read from <tt><font size=+1>/dev/random</font></tt>. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Prng</i> uses the native <a href="../man3/rand.html"><i>rand</i>(3)</a> pseudo-random number generator to |
| fill the buffer. Used with <i>srand</i>, this function can produce a |
| reproducible stream of pseudo random numbers useful in testing. |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| Both <i>genrandom</i> and <i>prng</i> may be passed to <i>mprand</i> (see <a href="../man3/mp.html"><i>mp</i>(3)</a>). |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Fastrand</i> uses <i>genrandom</i> to return a uniform <tt><font size=+1>unsigned long</font></tt> <i>x</i>, 0≤<i>x</i><<i>232-1. |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </i> |
| <i>Nfastrand</i> uses <i>genrandom</i> to return a uniform <tt><font size=+1>unsigned long</font></tt> <i>x</i>, |
| 0≤<i>x</i><<i>val</i>≤<i>232-1.<br> |
| </i> |
| </table> |
| <p><font size=+1><b>SOURCE </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <tt><font size=+1>/usr/local/plan9/src/lib9<br> |
| /usr/local/plan9/src/libsec/port<br> |
| </font></tt> |
| </table> |
| <p><font size=+1><b>SEE ALSO </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <a href="../man3/mp.html"><i>mp</i>(3)</a><br> |
| |
| </table> |
| <p><font size=+1><b>BUGS </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <i>Truerand</i> and <i>ntruerand</i> maintain a static file descriptor. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| To avoid name conflicts with the underlying system, <i>rand</i>, <i>lrand</i>, |
| <i>frand</i>, <i>nrand</i>, <i>lnrand</i>, and <i>srand</i> are preprocessor macros defined |
| as <i>p9rand</i>, <i>p9lrand</i>, and so on; see <a href="../man3/intro.html"><i>intro</i>(3)</a>.<br> |
| |
| </table> |
| |
| <td width=20> |
| <tr height=20><td> |
| </table> |
| <!-- TRAILER --> |
| <table border=0 cellpadding=0 cellspacing=0 width=100%> |
| <tr height=15><td width=10><td><td width=10> |
| <tr><td><td> |
| <center> |
| <a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a> |
| </center> |
| </table> |
| <!-- TRAILER --> |
| </body></html> |