On Jul 9, 10:28 pm, vulpes <ntv...@luukku.comwrote:
On Jul 3, 4:03 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Chelong wrote:
srand((int)time(0));
A quick solution is to use clock() instead of time().
Actually that's not recommended. clock() returns the number of clock
ticks since the _start of the program_ not since epoch or whatever.
Thus it will always give very similar values, because srand is usually
called at the start of the program. It will even give same values
almost always because usually the OS won't do a context switch (or any
other memory tricks) before the srand gets called and so a constant
number of instructions (and clock ticks) gets done before grabbing the
clock() value.
It's worse. The start time is only "related only to the program
invocation". At least under Solaris, the first call to clock()
always returns 0.
There are a number of system dependant solutions; under Unix,
for example, you can open "/dev/random", and read a couple of
bytes, or use gettimeofday to get the time in microseconds (but
the actual granularity won't be that fine). Hashing in a number
of various values, like the process id and the machine map
address, can be used as well.
As Chelong says, boost::random is _the_ choice. It uses Mersenne
Twister algorithm which is way better and faster than the basic C/C++
rand().
Boost random is a component, not a single class. You choose the
algorithm, and all of the algorithms require a seed, so using
Boost doesn't affect the problem the original poster had. I'm
curious about "faster" as well. A linear congruent generator is
just a multiplication and a modulo; the Mersenne twister seems
to do a lot more operations than just those. So if your machine
has good 64 bit hardware multiply and divide, the minimum
standard generator should be a lot faster. (This will obviously
depend on the machine, of course.)
Independantly of the speed, of course, rand() is often poorly
implemented, and you're better off using one of the Boost
algorithms, all of which have specified behavior, so you can
know what you are getting.
You should take care when using rand(), because some Windows machines
let the rand() value to be just a two byte short, which sucks.
For historical reasons, any number of machines declare RAND_MAX
to be 32767. For historical reasons, a lot of machines have a
very poor rand().
It doesn't matter usually though. Also, you shouldn't use the
same seed for more than one tenth or hundredth of the rand()'s
granularity.
What do you mean by granularity? Obviously, you don't want to
use more than a small part of the generator's cycle, but it's
fairly simple to combine generators if you need a longer cycle.
Or use the lagged fibonacci from boost. (In practice, it
depends on your needs. For most interactive game playing, I
would guess that the period of any of the Boost algorithms would
be sufficient.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient¨¦e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S¨¦mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34