# Thread: C random number generator function...

1. ## C random number generator function...

I have made a function that generates a random number and, from that number, pulls a name from a list if criteria is met. BUT, I am questioning its capability. I know with computers, the numbers generated are more psuedo-random than "pure" random.

The function is:

Code:
```int random(int copy_n, int n; char **names, char **save)
{
int            i, max;
double       j;

unsigned int   iseed = (unsigned int)time(NULL);
srand(iseed);

while(1){
for(i = 0; i < n; ++i){
/*      Generate a random number between 0 and 1. */
j = (double)(rand()/((double)RAND_MAX + 1));
//        printf ("The random number generated is %lf \n", j);
if(j <= .1){
//          printf("---> Value of random number is less than");
//          printf(" or equal .1 with value of %lf.\n", j);
strncpy(save[cap], names[i], 4);
max += 1;
if(max == copy_n){ // if max is equal to the amount to copy
break;
}
}
}

if(cap == copy_n) break;

}

return 0;
}```

2. Don't seed the pseudorandom number generator in that function. If there is a need to seed or re-seed, do it before calling that function.

3. so pass iseed in instead of doing it in the function?
any reason why it is bad to seed it in the function?

4. Originally Posted by towed
so pass iseed in instead of doing it in the function?
There's no point passing iseed to the function when you're not going to seed in the function.

Originally Posted by towed
any reason why it is bad to seed it in the function?
With such a time dependent seed, you would only want to seed the PRNG once throughout your program, unless you want to say, repeat a simulation. Otherwise, you would end up seeding with the same seed as not enough time may have passed, thus getting the same results, which would be undesirable if you don't actually want to repeat.

Anyway, what makes you question its capability? You could always pick a known PRNG, e.g., Mersenne Twister and use it.

5. Well, I will be running it for multiple cases and want different lists. Thus, seeding the PRNG (psuedo-random number generator I assume you are referring to) with time. Its just that, although the lists are different, I am getting a common first id that appears to be not randomly generated.

Further, I only want to take the lower 10 % of the list. Thus, I generate a random number and if it is less then 10 %, copy the name to the save name list. Thus, don't want identical saved lists.

6. Originally Posted by towed
Well, I will be running it for multiple cases and want different lists. Thus, seeding the PRNG (psuedo-random number generator I assume you are referring to) with time.
Seeding with time is fine. What I am talking about is not seeding more than once when you seed with time. Also, read Prelude's article on using rand().

Originally Posted by towed
I am getting a common first id that appears to be not randomly generated.
Since your code does not compile, I cannot tell if this is not merely a bug with your code.

Originally Posted by towed
Further, I only want to take the lower 10 % of the list. Thus, I generate a random number and if it is less then 10 %, copy the name to the save name list.
That does not "take the lower 10 % of the list". That just copies the current name to the save name list with about 10% of the time. The other 90% of the time, you just go on to the next iteration without doing anything else.

7. Originally Posted by laserlight
Seeding with time is fine. What I am talking about is not seeding more than once when you seed with time. Also, read Prelude's article on using rand().
Very interesting site... I see the use of "rand()/((double)RAND_MAX + 1)" there...

Since your code does not compile, I cannot tell if this is not merely a bug with your code.
No biggie... its not my full code, as that in itself is very long...

That does not "take the lower 10 % of the list". That just copies the current name to the save name list with about 10% of the time. The other 90% of the time, you just go on to the next iteration without doing anything else.
That is what I meant, sorry!