PDA

View Full Version : random number



pingus
6th July 2011, 16:20
how i create that i get random number betwen 1 and 32?
now command a = qrand() every time I run the program, it always returns the same numbers.

SixDegrees
6th July 2011, 16:30
You cannot generate random numbers with a function. Only pseudo-random numbers. qrand returns a deterministic sequence of pseudo-random numbers; the sequence is the same each time because you aren't seeding the sequence differently. You do this with qsrand(uint); you can use a time value for the argument if you want a different result each time.

This is true of all such functions, unless they are tied to a hardware source of random numbers such as those available on some CPUs. In general, such truly random sources are as necessary as they are rare.

Santosh Reddy
6th July 2011, 16:31
It is not possible to generate a perfect randon number sequence just using software, as software behaves always in a predictable pattern.

You can try time / second / milli second as a randonmize paramter to get better randon sequence.:)

Lesiok
6th July 2011, 18:22
Put this line In main()
qsrand(QDateTime::currentDateTime().toTime_t()
With this line every time pseudo-random generator will be initialised with another sequence.