hi,I want to know ,how to use generate value with qrand in qt?
hi,I want to know ,how to use generate value with qrand in qt?
Callonce on application start. It will seed the random number generator.qsrand(QTime::currentTime().msec())
Then generate the random numbers:
Qt Code:
int nextNumber = rand() % 15 + 1;To copy to clipboard, switch view to plain text mode
samira (29th June 2013)
did it work fine?
For converting range of
into 0-1,is it correct if I use this code?rand()(0-255)
Qt Code:
float nextNumber = (rand() % 10000) / 10000.0;To copy to clipboard, switch view to plain text mode
Added after 4 minutes:
yes it seems work correct,but i want to sure about it
Last edited by samira; 29th June 2013 at 13:53.
Yes, this is correct.
Assuming you want an integer value in the range 0 to N-1 for N < RAND_MAX then you just do this:
However, it seems you wanted a random real value in the range 0 to 1.0, which qrand() does not produce directly:
Be aware that RAND_MAX is typically fairly small and may give wide gaps between possible "random" floats.Qt Code:
qreal value = static_cast<qreal>(qrand()) / RAND_MAX ;To copy to clipboard, switch view to plain text mode
Neither approach is suitable for cryptographic applications or where the uniformity of the random distribution is very important.
Bookmarks