I
Intiha
Hello all,
I am trying to generate random seeds for my simulations.
currently i was using srand(time(NULL); for this purpose.
But for confidence in my results i ran it using a script in a loop.
Since the time b/w execution is very similar, many simulation runs
resulted in exact same results.
Is there a better way of seeding the random number generator in c/c++
than time(NULL).
PS: I did some search and found this small snippet on this group: but i
am not
convinced that it will still result in much different seeds if the
value returned by time(NULL) is the same... any comments?
#include <limits.h>
#include <stdlib.h>
#include <time.h>
void randomize(void)
{
time_t timeval = time(NULL);
unsigned char *ptr = (unsigned char *)&timeval;
unsigned seed = 0;
size_t i;
for (i = 0; i < sizeof timeval; i++)
seed = seed * (UCHAR_MAX+2U) + ptr;
srand(seed);
}
I am trying to generate random seeds for my simulations.
currently i was using srand(time(NULL); for this purpose.
But for confidence in my results i ran it using a script in a loop.
Since the time b/w execution is very similar, many simulation runs
resulted in exact same results.
Is there a better way of seeding the random number generator in c/c++
than time(NULL).
PS: I did some search and found this small snippet on this group: but i
am not
convinced that it will still result in much different seeds if the
value returned by time(NULL) is the same... any comments?
#include <limits.h>
#include <stdlib.h>
#include <time.h>
void randomize(void)
{
time_t timeval = time(NULL);
unsigned char *ptr = (unsigned char *)&timeval;
unsigned seed = 0;
size_t i;
for (i = 0; i < sizeof timeval; i++)
seed = seed * (UCHAR_MAX+2U) + ptr;
srand(seed);
}