Manipulating Rand()

I have a question: so I know of the way to have rand() come up with a new random number every second, is there a way to have it give me a new random number, say, every millisecond?
rand() gives you a new random number every time you call it. Time has nothing to do with it.

The only way time has anything to do with it is when you seed the RNG with srand()... which you should only be doing once. The mistake you're making is probably seeding every time you generate a number, which is wrong.

1
2
3
4
5
6
7
8
int main()
{
    srand( (unsigned)time(0) );  // seed ONLY ONCE

    // get 2 random numbers between [0..9]
    int a = rand() % 10;
    int b = rand() % 10;
}
Last edited on
Perhaps I worded my problem wrong. So I seed rand once, but I generate random numbers in a loop, in essence getting the same number for several thousand times until rand() switches to a new number, and then spits it out for another few thousand times. I want to know how to change this, so that, in this case, rand() would switch random numbers every millisecond. The case is that I am calling rand() repeatedly.
It sounds like you are seeding inside the loop? Perhaps you could post your code that is in question?
in essence getting the same number for several thousand times until rand() switches to a new number


Again... rand() "switches to a new number" every time you call it. What you are describing is impossible unless:

- you are seeding every time you call rand
or
- you are not calling rand each time.


kevinkjt2000 is right, it would help if you posted code so we can see what you are doing. You must be doing something incorrectly.
Topic archived. No new replies allowed.