Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ (2) — Plan9 3rd Edition

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

cons(3)

RAND(2)

NAME

rand, lrand, frand, nrand, lnrand, srand, truerand, n_truerand, fastrand − random number generator

SYNOPSIS

­#include <u.h>
­#include <libc.h>

intrand(void)

longlrand(void)

doublefrand(void)

intnrand(int val)

longlnrand(long val)

voidsrand(long seed)

ulongtruerand(void)

intn_truerand(int val)

ulongfastrand(void)

DESCRIPTION

­Rand returns a uniform pseudo-random number x, 0≤x<215. 

­Lrand returns a uniform ­long x, 0≤x<231. 

­Frand returns a uniform ­double x, 0.0≤x<1.0, This function calls ­lrand twice to generate a number with as many as 62 significant bits of mantissa. 

­Nrand returns a uniform integer x, 0≤x<val.  ­Lnrand is the same, but returns a long. 

The algorithm is additive feedback with:

x[n] = (x[n−273] + x[n−607]) mod 231

giving a period of 230 × (2607 − 1). 

The generators are initialized by calling ­srand with whatever you like as argument.  To get a different starting value each time,

srand(time(0))

will work as long as it is not called more often than once per second.  Calling

srand(1)

will initialize the generators to their starting state. 

­Truerand returns a random unsigned long read from /dev/random.  Due to the nature of /dev/random, truerand can only return a few hundred bits a second. 

­N_truerand returns a uniform integer x, 0≤x<val. 

­Fastrand is a pseudo random number generator which is seeded and periodically scrambled using truerand. It is approximately 1000 times faster than ­truerand and is intended for security software that requires a larger stream of unguessable data. 

SOURCE

­/sys/src/libc/port/rand.c
­/sys/src/libc/9sys/truerand.c

SEE ALSO

cons(3)
 

Plan 9  —  March 27, 2001

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026