Is srand(time(0)); an effective solution for seeding rand(), or is there
any better approach?
There are 3 functions which are better I think:
1.gettimeofday( ): More precision (microsecond clock)
2.There is a function that countts the time since the system is running,
but I don't know what's it's name, just google for it
3. Timestep counter: called by ASM function readtsc, e.g. use that
inline asm function (GCC):
inline volatile unsigned long ReadTSC()
{
unsigned long tsc;
asm("rdtsc":"=A "(tsc));
return tsc;
}
The amount of timesteps (TS) per second is the reciprocal of the clock
frequency. Kind regards, Hans
>Is srand(time(0)); an effective solution for seeding rand(), or is there
>any better approach?
There are 3 functions which are better I think:
1.gettimeofday( ): More precision (microsecond clock)
2.There is a function that countts the time since the system is running,
but I don't know what's it's name, just google for it
time()?
3. Timestep counter: called by ASM function readtsc, e.g. use that
inline asm function (GCC):
[...]
Of course it has to be said that neither 1. nor 3. is portable (unlike
the original solution.)
>>Hans Mull wrote:
>>>2.There is a function that countts the time since the system is
>>>running, but I don't know what's it's name, just google for it
>>>
>>time()?
>>
>>
>I am afraid he means clock(), and its use for seeding srand() is worse
>than time(0).
>
Neither. I believe Unix has something like "system uptime" you
could get, and Windows probably has something similar. But I
wouldn't use those.
While I'm not an expert on the subject I do not think that the source of
the seed is very important as long as it is has a high probability of
not being identical on multiple invocations. The reason is that rand()
is (usually) not a very good PSNG so it should only be used in places
where good random number are required anyway.
>>Matthias Buelow wrote:
>>>Hans Mull wrote:
>>>>2.There is a function that countts the time since the system is
>>>>running, but I don't know what's it's name, just google for it
>>>>
>>>time()?
>>>
>>>
>>I am afraid he means clock(), and its use for seeding srand() is
>>worse than time(0).
>>
>Neither. I believe Unix has something like "system uptime" you
>could get, and Windows probably has something similar. But I
>wouldn't use those.
>
While I'm not an expert on the subject I do not think that the source
of the seed is very important as long as it is has a high probability
of not being identical on multiple invocations. The reason is that
rand() is (usually) not a very good PSNG
What does 'S' stand for in PSNG? Thanks.
so it should only be used in
places where good random number are required anyway.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
>>Ioannis Vranos wrote:
>>>Matthias Buelow wrote:
>>>>Hans Mull wrote:
>>>>>2.There is a function that countts the time since the system is
>>>>>running, but I don't know what's it's name, just google for it
>>>>>
>>>>time()?
>>>>
>>>>
>>>I am afraid he means clock(), and its use for seeding srand() is
>>>worse than time(0).
>>>
>>Neither. I believe Unix has something like "system uptime" you
>>could get, and Windows probably has something similar. But I
>>wouldn't use those.
>>
>While I'm not an expert on the subject I do not think that the source
>of the seed is very important as long as it is has a high probability
>of not being identical on multiple invocations. The reason is that
>rand() is (usually) not a very good PSNG
>
What does 'S' stand for in PSNG? Thanks.
Opps, should be an 'R', as in Pseudo Random Number Generator. And with
'usually' I mean that while there is nothing (to my knowledge) that
prevents rand() from using an algorithm that produces good random
numbers most implementations do not. Which is probably one of the
reasons for the new PRNGs in the next version of the standard.
On Mar 10, 5:17 pm, Ioannis Vranos <ivra...@nospam .no.spamfreemai l.gr>
wrote:
Matthias Buelow wrote:
Hans Mull wrote:
2.There is a function that countts the time since the
system is running, but I don't know what's it's name, just
google for it
time()?
I am afraid he means clock(), and its use for seeding srand()
is worse than time(0).
He said since the system was running, not since the program was
running. (In fact, on the systems I regularly use, the first
call to clock() in a given process will always return 0. Not
very random as random seeds go.)
Of course, if you're not worried about portability:
std::ifstream src( "/dev/random", std::ios::binar y ) ;
unsigned s ;
src.read( reinterpret_cas t< char* >( &s ), sizeof( s ) ) ;
srand( s ) ;
(but with added error handling) is about as good as you can get.
In fact, what I generally use is something like:
std::ifstream src( "/dev/random", std::ios::binar y ) ;
unsigned s ;
if ( ! src.read( reinterpret_cas t< char* >( &s ), sizeof( s) ) ) {
s = time( NULL ) ;
}
srand( s ) ;
Alternatively (depending on the application), I might try mixing
in some other more or less random sources: the pid, the
machine's IP address, etc. But generally speaking, it's not
worth the bother. (Especially for srand/rand. If you're using
a known good generator, it might be worthwhile.)
A lot depends on why you want the random numbers. If you're
using them to seed the generator for backing off in case of
collision on a LAN, for example, time() is probably not a very
good source; if all of the machines are powered on at the same
time (quite possible, if they all are on the same power supply),
then your collision handling won't work well at all. (In that
case, using the machines IP or MAC address is a much better
idea.) If you're starting a couple of separate processes, each
with it's own generator, for some sort of simulation, time()
probably isn't a good idea either (since it would result in all
processes using the same seed)---in that case getpid() is
suggested. In my case, the code is in a library, and I don't
know what it is going to be used for. So I use the above,
except that in place of time(), I use a hash of time(),
getpid(), and the machine's IP address, hoping to cover all
eventualities.
But as I said, for most uses (games, etc.), time() alone is
perfectly fine.
--
James Kanze (GABI Software) email:james.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Comment