Re: Portable random number generator
On Nov 10, 4:12 am, Gus Gassmann <horand.gassm...@googlemail.com>
wrote:
I am collaborating on a rather large project with complicated XML
files (some objects nest ten levels deep) and corresponding data
handling challenges. I want to do proper testing of the (C++) code and
decided that the only way to go is random tests. So now I am looking
for a random number generator with the following properties:
1. Portability.
2. Random starting points.
3. Replicability on demand.
I presume this means that I would seed the RNG based on the clock, but
keep a copy of the seed that I could optionally use at the start in
case I found a problem on a previous run.
Statistical properties are of lesser importance.
I presume I am not the first person to attempt this and am hoping to
find some guidance here. Both C and C++ would be OK for the RNG, hence
the cross-post.
There are a number of implementations available on the network;
boost has some, for example. And it's not difficult to
implement the minimum generator yourself.
With regards to the seed: time() is the classical solution, but
depending on the context in which your program runs, it may not
suffice. On Unix platforms, I'll read a couple of bytes from
/dev/random. Otherwise, munging in things like the host IP
address and the process id may be necessary to ensure
uniqueness. (And once you've got a seed, log it so you can use
it in case of problems, like you said.)
--
James Kanze
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.