ran1 Problems


x97mdr
11-09-2004, 12:36 PM
Hi all,

I'm having the same problem another user no this forum posted about with ran1, that is the first 8 numbers produced are ALWAYS the same.

I have tried various things.

1. Calling ran1 with -1 once, then after that calling it with a time as the seed.

double get_rand(void)
{
static long seed = -1;
static int first_time = 1;

if (first_time)
{
first_time = 0;
seed = -1;
ran1(&seed);

seed = time(NULL);
}

return ran1(&seed);
}

2. Then I tried it with a random number as a seed

double get_rand(void)
{
static long seed = -1;
static int first_time = 1;

if (first_time)
{
first_time = 0;
seed = -1;
ran1(&seed);

seed = rand();
}

return ran1(&seed);
}

3. Then I tried it with 1 used all the time

double get_rand(void)
{
static long seed = -1;
static int first_time = 1;

return ran1(&seed);
}

And with all THREE methods I get the same 8 numbers for the first 8 digits. Obviously this is a bit of a problem. I tried looking at the Examples given in the example book but they were no help, it seems they use the third method, only call with negative 1 as the seed every time a random number is required.

Please help!! Is there something i'm doing wrong in my usage of ran1, I copied and pasted the ran1 code right off of the 2.10 version CD ...