Re: Portable random number generator
On Nov 10, 10:21 pm, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
On Wed, 2010-11-10, Juha Nieminen wrote:
Victor Bazarov <v.baza...@comcast.invalid> wrote:
I am afraid to ask... Why not use 'srand/rand' pair of functions?
It's indeed the standard solution, but only if the
horrendous quality of the typical std::rand() implementation
isn't a problem.
Which ones are you thinking of, specifically? And what's horrendous
about them?
Historically, many rand() were horrible; I don't know if it's
still a problem, but the (non-normative) example for rand() in
C90 was horrible, and it corresponded to a widely used
implementation. (The C90 committee didn't invent the
implementation; they just copied the most widely used one.)
Today, the obvious solution is to use the random generators from
Boost: they have a number of them, with a common interface, so
you can choose the balance between "randomness" and speed which
corresponds to your needs.
For the seed, the choice of time() is OK for programs running
independently on a machine. More generally, however, something
more elaborate may be desirable. I'd just read however many
bytes I needed from /dev/random under Unix; otherwise, you can
munge in such values as the machine IP or (better) MAC address,
the process id, etc., to get something truely unique.
--
James Kanze