Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

random-bsd

Random numbers using the FreeBSD generator.

Overview

This egg provides a good-quality random number generator derived from FreeBSD's generator, which is also used in other operating systems such as Mac OS X. It provides 53 bits of precision.

It can be used as a drop-in replacement for random in the core extras unit. Core random uses rand(3) and has serious deficiencies on Mac OS X and elsewhere, and is little more than a toy on Windows and Solaris. To override the core generator, simply

(use random-bsd)

This egg does not try to detect or use any random number generator provided natively by your system.

API

Generating random numbers

[procedure] (random-integer N)

Return a random integer in the range 0 .. N-1, where N is a fixnum or an integer flonum.

When N is a fixnum, this is equivalent to random-fixnum.

Otherwise, the result is returned as an integer flonum with 52 bits of precision. No attempt is made to coerce the result to an exact number.

[procedure] (random-fixnum N)

Return a fixnum in the range 0 .. N-1, where N is an exact integer.

On a 32-bit system, the range is 0 .. 2^30-2.

On a 64-bit system, the range is 0 .. 2^62-2. There are at most 53 bits of precision, so when N >= 2^53 there will be gaps in the range.

[procedure] (random N)

Alias for random-fixnum. This allows you to override the core random with

(use random-bsd)
[procedure] (random-real)

Return a random floating-point number in the range 0.0 <= x < 1.0. The result will have double precision (53 bits).

Randomizing the generator

[procedure] (randomize [seed])

Seed the generator with integer SEED. If seed is not provided or #f, the generator is seeded with a combination of the current time (seconds and milliseconds) and process ID.

(randomize) is automatically called at module load time so it is not normally necessary to call it again.

[procedure] (randomize/device)

Seed the generator with external entropy. Currently, random data is obtained by reading /dev/random and used to initialize the generator state. If /dev/random is not present, it falls back to seeding with the current time and PID. This is like calling srandomdev(3).

On some systems, reading from /dev/random will block if there is insufficient entropy in the pool.

About this egg

Author

Jim Ursetto, Ursetto Consulting, Inc.

The random number generator is taken from FreeBSD.

Version history

0.2
(2011-07-09) Upgrade precision to 53 bits; speedup for exact random-integer
0.1
(2011-07-07) Initial release

License

Copyright (c) 2011, Ursetto Consulting, Inc. MIT license. Full license details here.

random.c is Copyright (c) 1983, 1993 The Regents of the University of California.