
Originally Posted by
samira
hi,I want to know ,how to use generate value with qrand in qt?
Assuming you want an integer value in the range 0 to N-1 for N < RAND_MAX then you just do this:
int value = qrand() % N;
int value = qrand() % N;
To copy to clipboard, switch view to plain text mode
However, it seems you wanted a random real value in the range 0 to 1.0, which qrand() does not produce directly:
qreal value = static_cast<qreal>(qrand()) / RAND_MAX ;
qreal value = static_cast<qreal>(qrand()) / RAND_MAX ;
To copy to clipboard, switch view to plain text mode
Be aware that RAND_MAX is typically fairly small and may give wide gaps between possible "random" floats.
Neither approach is suitable for cryptographic applications or where the uniformity of the random distribution is very important.
Bookmarks