I
Ioannis Vranos
Is srand(time(0)); an effective solution for seeding rand(), or is there
any better approach?
any better approach?
Ioannis said:Is srand(time(0)); an effective solution for seeding rand(), or is
there any better approach?
Victor said:Unless you're doing it too often (which might just lead to the same
seed passed to 'srand'), it's commonly accepted, provided that you
actually cast the result of 'time(0)' to unsigned (for portability's
sake):
srand(unsigned(time(0)));
Ioannis said:Since the function prototype is
void srand(unsigned int seed);
doesn't an implicit conversion to unsigned int take place?
There are 3 functions which are better I think:Ioannis said:Is srand(time(0)); an effective solution for seeding rand(), or is there
any better approach?
[...]Hans said: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):
Matthias said:time()?
Ioannis said: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.
Erik said: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.
What does 'S' stand for in PSNG? Thanks.
Neither. I believe Unix has something like "system uptime" you
could get, and Windows probably has something similar. But I
wouldn't use those.
John said:"Victor Bazarov"
On a system where RAND_MAX is 32,767, this means there are only 32767
unique numbers available in the first place, and
srand(WhicheverIsUsed) merely rearranges the order of the numbers 0
to 32767.
If one wanted more than 32767 numbers (say, for randomly-populating
100,000 xyz coordinates), then regardless of how often one rearranged
them by re-seeding the PRNG, how would one do that?
Would it mean
having to select a non-integrated (to C++, for example) PRNG, thus
risking non-portability?
No, that's not correct. The numbers are not guaranteed to be unique
during picking RAND_MAX numbers. Some will repeat, some will not
come up.
There is no need to reseed the generator. The sequence can be good
enough for picking out of many more (that's controlled by the
generator's "period" characteristic and needn't be linked to the
RAND_MAX value at all. Even if you run into a shorter period PRNG,
you can always generate several sequences and then interleave them
with each other to get a relatively random final set.
I am afraid he means clock(), and its use for seeding srand()
is worse than time(0).
No, that's not correct. The numbers are not guaranteed to be
unique during picking RAND_MAX numbers. Some will repeat,
some will not come up.
There is no need to reseed the generator. The sequence can be
good enough for picking out of many more (that's controlled by
the generator's "period" characteristic and needn't be linked
to the RAND_MAX value at all.
Even if you run into a shorter period PRNG, you can always
generate several sequences and then interleave them with each
other to get a relatively random final set.
There are many good PRNGs on the 'net, pick one that suits
your needs, but first you need to prove that the built-in is
not going to suit you.
There is no indicator to that yet, AFAICT (aside from your
doubt in its capabilities).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.