Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

time() function and random numbers

007age007age Member Posts: 2
I am trying to create a random number generator using time as the seed, but it seems that the time function I am using just uses seconds. Is there any way for me to use milisecs?

Here is a snippet of the code I am using:

int x;
srand(time(0));
y = rand();

return y;

Anything I can do? Is there a better way to generate a random number? I eventually only want a 1 or a 0 (I am mod-ing by 2 and if the random number is even, then I make it a 1, etc...)

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    Are you always using 'srand()' before ANY call to 'rand()'? If so, this may be a problem. Try to use 'srand()' once at the beginning of 'main()' and then use only 'rand()' after this... The CPU is so fast that the numbers will all be equal a lot more then you need to.

  • tmlutmlu Member Posts: 15
    : Are you always using 'srand()' before ANY call to 'rand()'? If so, this may be a problem. Try to use 'srand()' once at the beginning of 'main()' and then use only 'rand()' after this... The CPU is so fast that the numbers will all be equal a lot more then you need to.
    :
    :
    Hi,
    I have the same problem. I call srand() only once at the begginning, and my computer do generate a sequence of random numbers well. But it seems that the first number is almost the same number all the time, which is not the case for those numbers following.

    -Lu.
  • 007age007age Member Posts: 2
    : Are you always using 'srand()' before ANY call to 'rand()'? If so, this may be a problem. Try to use 'srand()' once at the beginning of 'main()' and then use only 'rand()' after this... The CPU is so fast that the numbers will all be equal a lot more then you need to.
    :
    :
    Well, what do you know! That was my problem, now I am getting really random numbers!

    Thank you very much.
Sign In or Register to comment.