# New Random # in less than 1 sec...?

• 01-08-2002
Malikive
New Random # in less than 1 sec...?
I read the FAQ and several posts, but can anyone tell me how to generate a random number in less than 1 second? I'm making a game where I need random numbers calculated in less than 1 second and it seems that with Srand(time(NULL)), Randomize(); and rand()%#, it can only get a random number every second. When determining how hard you hit is based on random values and other stats, it sucks getting the same values if you attack twice in less than 1 second and so forth.. Also running into enemies is determined randomly and doing it every second doesn't work cuz you move at least 10 spaces before it determines another number. Anyone help?
• 01-08-2002
Sorensen
You only need to call srand() once, and not everytime you need a random number. If this doesn't help post your code.
• 01-08-2002
taylorguitarman
you can call rand() as fast as you want and it will give you a different "random" number each time. That's the whole concept behind random numbers.
this is works just fine.
Code:

```#include <iostream.h> #include <stdlib.h> #include <time.h> int main() {  // this seeds rand()  // you can put anything you want as a seed but typically you use system time  srand(time(NULL));  // now make random numbers  for(int i = 0; i < 20; ++i)  {   cout << rand() << endl;  }  return 0; }```
Now unless you have a very slow computer each number should be generated in much less than 1 second and they will statistically be different.
Note: As in all of the other posts in the past week regarding random numbers (perhaps people should use the search feature?) rand() is a pseudo random generator, which means that it will produce the same sequence with a given seed. Each number will of course be random though.
• 01-13-2002
Malikive
Cool, it worked, thanks :)
Hey, Thanks Sorensen, that worked, my comp is fast enough and I thank you all for the help but yeah, I was calling the srand() right before it used rand()% and that was in a loop, Thanks guys!