Re: Random number generator.
On May 31, 8:00 pm, Dar=EDo Griffo <dario.griffo.lis...@gmail.com>
wrote:
On May 31, 2:09 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
This is a minor feature I want to add to my application
(which is just using rand() with no predictability right
now). Therefore, to be honest, I am not interested in doing
any major amount of work or research. I am wondering if
anybody knows of a decent implementation that is easy to
drop in to existing code (there doesn't appear to be
anything in the STL, is there?).
From man 3 rand
POSIX.1-2001 gives the following example of an implementation of
rand() and srand(), possibly useful when one needs the same sequence
on two different machines.
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
Posix just copied this from the C standard. It's known to be
very poor.
If you're interested in linear congruent generators, you should
at least read "Random Number Generators: Good Ones Are Hard to
Find", by Park and Miller (CACM, Oct. 1988). If you want
portable repeatability, then you can just use the generator they
suggest. I've corrected some of the known weaknesses in this
generator (and significantly extended its period) in my own
Random class (available at my site), and Boost has a large
collection of fully specified random generators, some with
characteristics far better than mine. To be frank, I'd
recommend using one of the Boost generators rather than my own.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34