This is the mail archive of the
libc-hacker@sourceware.cygnus.com
mailing list for the glibc project.
draft random-number-generator API
- To: libc-hacker@cygnus.com
- Subject: draft random-number-generator API
- From: Zack Weinberg <zack@rabi.phys.columbia.edu>
- Date: Thu, 03 Sep 1998 18:36:26 -0400
We've got three different APIs for random number generation (ANSI/POSIX,
BSD, SVID) none of which provides the flexibility you want if you're doing
serious random-number work. Furthermore most of the algorithms used are
quite lame.
I've devised a new API which I think solves these problems. It looks like
this: There are a bunch of accessor functions that give you random numbers.
You can get signed or unsigned 32-, 48-, or 64-bit integers with uniform
distribution, or floats/doubles/long doubles in the ranges (-1, 1) or [0, 1)
with uniform, normal, Poisson, ... distribution.
All the accessor functions take a single argument which is a pointer to a
"state block." The state block encodes the RNG algorithm to use, any
tunable parameters of that algorithm, and the current state of the
generator. If you pass NULL for the state block you get a default.
You create a state block with another function, which takes a selector for
the algorithm, an initial seed, and any tunable parameters. It returns you
a pointer to malloced memory. You can reseed the block with yet another
function, or set the default state block to one of your choosing.
The neat thing about this is it's sufficiently general that we can implement
all the historical interfaces in terms of it. So if you have a program
written to use rand(), and you want a better RNG for it, you can just add
one line to the beginning. [Of course, some of the interfaces, such as
rand_r() and lcong48(), force the use of particular algorithms.]
Comments? (Obviously this isn't for 2.1, I'll do it up as an add-on
initially)
zw