Random number

Hi, I am trying to generate random numbers, but I want to generate a new number on each mouse click. I have used srand(time(0)); for this, but the number is obviously the same for a second if i click quickly. I am wanting a completely different number each time. I know you are not meant to use rand more than once in a program, so I was wondering if there is anyway to generate different numbers?

obviously the same for a second if i click quickly
Clicking quickly should not be a problem at all.

If you want to seed srand with time(0), you do it only once, preferably at the beginning of the program.
Don't call srand more than once.

Aside from that, if you don't want to use the current time as your seed, you have to come up with another source of entropy. For example, you could measure the time it takes from start of program to the time it takes a person to click, in milli- or microseconds, and use that as a seed. There's creative ways to do stuff, if you just need simple RNG.
Last edited on
Hello kmce,

I think you have it a bit backwards here. You only use "srand(...)" once in a program usually near the top of"main" before your other code. Then you can use "rand()" as often as you like within a program.

When you can or start using the header file "crono" there are more different RNGs can use.

Calling "rand()" will generate a random number. The first time it is based on the number that "sranr(...)" sets up. after that the next number that "rand" generates is based on the last that "rand" generated.

Not seeing your code I can not say where you went wrong.

Hope that helps,

ANdy
Ah, OK I was confused with the rand and srand. Once I moved the srand into my main, and took it out elsewhere, everything worked as planned. Thank you both very much. :)
Topic archived. No new replies allowed.