PDA

View Full Version : Random Number Qt MacOs



Daniela
15th October 2008, 11:20
Hello!
:confused:

I'm doing a random number in MacOs, but it returns always the same number. i use this:


#include <ctime> // To seed random generator

// Randomize
qsrand( time(0) );

//to get a random number i use this funtion:

int myClasse::randInt( int a, int b )
{
qsrand( time(0) );
return a + rand() % ( b - a + 1 );
}

but it always return the same number. Why this happens?


Thanks for read this post...

Daniela
16th October 2008, 12:01
:crying:
Now i "reset" the qsrand, and for any time i start my application, the random number is not the last one. Thats ok.

But in the same time the application is running, if i want to get a new Random number several times, it returns always the same number.

How can i random the random number process to return always different numbers?
Is it possible?
:confused:

^NyAw^
16th October 2008, 17:40
Hi,

You have to call "srand" only the first time that your application starts. Then every call to "rand" will get you different random values.