This is the mail archive of the
libc-hacker@sourceware.cygnus.com
mailing list for the glibc project.
Re: draft random-number-generator API
- To: zack@rabi.phys.columbia.edu
- Subject: Re: draft random-number-generator API
- From: Mark Kettenis <kettenis@phys.uva.nl>
- Date: Fri, 4 Sep 1998 10:05:19 +0200 (CEST)
- CC: libc-hacker@cygnus.com
- References: <199809032236.SAA28245@rabi.phys.columbia.edu>
X-Organisation: Faculty of Mathematics, Computer Science, Physics & Astronomy
University of Amsterdam
Valckenierstraat 65
NL-1018 XE Amsterdam
The Netherlands
X-Phone: +31 20 525 5663
X-Telex: 10262 hef nl
X-Fax: +31 20 525 5788
Date: Thu, 03 Sep 1998 18:36:26 -0400
From: Zack Weinberg <zack@rabi.phys.columbia.edu>
Sender: owner-libc-hacker@cygnus.com
Precedence: bulk
Content-Type: text
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)
You might want to take a look at GSL, The GNU Scientific Library. It
is still in development but one of the parts that seem to be almost
finished is the part that deals with RNGs. As far as I can see, it
uses more or less the same approach as you describe above. You can
find it on `alpha' (ftp://alpha.gnu.org/gnu/gsl-0.4.tar.gz).
IMHO this doen't really belong in libc, but in a seperate library.
GSL seems to be a better place for it.
Mark