Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of qsrand, random method that is not random

Tags:

random

qt

I'm having a strange problem here, and I can't manage to find a good explanation to it, so I thought of asking you guys :

Consider the following method :

int MathUtility::randomize(int Min, int Max)
{
    qsrand(QTime::currentTime().msec());

    if (Min > Max)
    {
        int Temp = Min;
        Min = Max;
        Max = Temp;
    }
    return ((rand()%(Max-Min+1))+Min);
}

I won't explain you gurus what this method actually does, I'll instead explain my problem :

I realised that when I call this method in a loop, sometimes, I get the same random number over and over again... For example, this snippet...

for(int i=0; i<10; ++i)
{
    int Index = MathUtility::randomize(0, 1000);
    qDebug() << Index;
}

...will produce something like :

567 567 567 567...etc...

I realised too, that if I don't call qsrand everytime, but only once during my application's lifetime, it's working perfectly...

My question : Why ?

like image 672
Andy M Avatar asked May 04 '10 17:05

Andy M


People also ask

What is the purpose of srand() and time()?

srand() Standard PracticesThe time() function returns the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e. the current unix timestamp). As a result, the value of seed changes with time. So every time we run the program, a new set of random numbers is generated.

What is the use of srand time null))?

Hence, you can initialize the seed of random in each run of the code to get a different random result by srand . Using time(NULL) to set a different seed of random through srand . Show activity on this post. srand is a random number generator function which will randomize the number produced by rand function.

What is the purpose of using function srand()? to set the seed of rand() function to enable rand() function to improve efficiency of rand() to generate random number?

The function srand () is used to provide seed for generating random numbers while rand () function generates the next random number in the sequence.

What is the use of srand function in c?

srand() uses its argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is not called, the rand() seed is set as if srand(1) was called at program start. Any other value for seed sets the generator to a different starting point.


2 Answers

Because if you call randomize more than once in a millisecond (which is rather likely at current CPU clock speeds), you are seeding the RNG with the same value. This is guaranteed to produce the same output from the RNG.

Random-number generators are only meant to be seeded once. Seeding them multiple times does not make the output extra random, and in fact (as you found) may make it much less random.

like image 151
Michael Myers Avatar answered Oct 13 '22 01:10

Michael Myers


If you make the call fast enough the value of QTime::currentTime().msec() will not change, and you're basically re-seeding qsrand with the same seed, causing the next random number generated to be the same as the prior one.

like image 39
fbrereto Avatar answered Oct 13 '22 01:10

fbrereto