Next: , Previous: , Up: random   [Index]


49.6 Device–based random numbers

In Unix–like operating systems, /dev/random is a special file that serves as a true random number generator or as a pseudorandom number generator. It allows access to environmental noise collected from device drivers and other sources. Not all operating systems implement the same semantics for /dev/random14.

Function: make-random-source/device
Function: make-random-source/device device

Create and return a new randomness source using the specified device to generate random integers in the range [0, 2^{32}). When device is not specified, it defaults to /dev/urandom.

We have to remember that reading /dev/random will block if not enough entropy is available, waiting for more randomness to be provided by the system. This is why, by default, this function will use /dev/urandom, which never blocks but provides randomness of lesser quality.

The state returned by random-source-state-ref is a Scheme vector of length 7, whose first value is the symbol random-source-state/device. The other values are the device pathname, a cache vector of values, the index of the next value to be extracted from the cace vector.

Parameter: random-device-cache-length

Device–based randomness sources cache read bytes into a bytevector of fixed size. This parameter allows us to select the size at source construction time. It is preset to 4096.

Low level API for device randomness sources

Function: %random-bytevector device number-of-bytes
Function: random-bytevector number-of-bytes
Function: urandom-bytevector number-of-bytes

Read number-of-bytes from the specified device on the file system and return them in a newly allocated bytevector. random-bytevector is a specialised version reading bytes from /dev/random. urandom-bytevector is a specialised version reading bytes from /dev/urandom.

Function: %random-bytevector! device bv
Function: random-bytevector! bv
Function: urandom-bytevector! bv

Fill the bytevector bv with bytes read from the specified device on the file system; return bv itself. random-bytevector! is a specialised version reading bytes from /dev/random. urandom-bytevector! is a specialised version reading bytes from /dev/urandom.


Footnotes

(14)

See http://en.wikipedia.org/wiki/Urandom, last verified Fri Jun 26, 2009.


Next: , Previous: , Up: random   [Index]