identifier qrand is undefined
-
@Chris-Kawa thanks, changing it to
QRandomGenerator::global()->generateDouble()
fixed the widget not being show correctly.text += QChar(chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)]);
I'm getting an exception index out out of range
@Roberrt said in identifier qrand is undefined:
(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)
Replace this whole thing with
QRandomGenerator::global()->bounded(chars.size())
-
Yes, it's deprecated.
Use QRandomGenerator::global() instead. The doc has example how to make a random color. -
Yes, it's deprecated.
Use QRandomGenerator::global() instead. The doc has example how to make a random color.@Chris-Kawa thank you, and in this case?
qsrand(QTime::currentTime().msec());
-
@Chris-Kawa thank you, and in this case?
qsrand(QTime::currentTime().msec());
@Roberrt If you read that documentation I linked you'll see that
QRandomGenerator::global()
is already securely seeded, so you don't need to seed it again. -
@Chris-Kawa thank you, and in this case?
qsrand(QTime::currentTime().msec());
-
Im trying to update this project: https://github.com/omkar-developer/QtCaptcha
But when i replace rand to QRandomGenerator::global() i get many compile errors
-
Im trying to update this project: https://github.com/omkar-developer/QtCaptcha
But when i replace rand to QRandomGenerator::global() i get many compile errors
-
Im trying to update this project: https://github.com/omkar-developer/QtCaptcha
But when i replace rand to QRandomGenerator::global() i get many compile errors
@Roberrt Please please read that page I linked. It's the least you can do.
QRandomGenerator::global()
returns an instance of the generator. To get a number call
QRandomGenerator::global()->generate()
-
@Chris-Kawa said in identifier qrand is undefined:
QRandomGenerator::global()->generate()
Im already using QRandomGenerator::global()->generate() it was just a typo
-
@Chris-Kawa said in identifier qrand is undefined:
QRandomGenerator::global()->generate()
Im already using QRandomGenerator::global()->generate() it was just a typo
@Roberrt Then what's the error?
-
@Roberrt Then what's the error?
@Chris-Kawa i've been able to fix some, but this one don't:
text += chars[(qrand() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
to
text += chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
Now im getting this error:
no operator "+=" matches these operands
I don't know if replacing these rands messed something up in the code, I'm getting a different layout than in her GitHub pictures
probably I shouldn't remove this line as @JonB suggested
void Captcha::randomize() { //qsrand(QTime::currentTime().msec()); }
-
@Chris-Kawa i've been able to fix some, but this one don't:
text += chars[(qrand() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
to
text += chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
Now im getting this error:
no operator "+=" matches these operands
I don't know if replacing these rands messed something up in the code, I'm getting a different layout than in her GitHub pictures
probably I shouldn't remove this line as @JonB suggested
void Captcha::randomize() { //qsrand(QTime::currentTime().msec()); }
-
@Roberrt
qsrand()
seeds forqrand()
. It has nothing to do withQRandomGenerator
.RAND_MAX
is also forqrand()
. Just casting it toqreal
does not mean it's suitable to use withQRandomGenerator
.Why are you indexing an array by a
qreal
expression? -
This expression
(qrand() / (qreal) RAND_MAX)
gives you a random double number in range 0..1.
So you can just replace this whole thing withQRandomGenerator::global()->generateDouble()
, which does the same. Same for the lines above it.
Or better yet - remove that whole expression with bounded() which will give you a random index in a range and you won't have to do any of those shenanigans.Now im getting this error: no operator "+=" matches these operands
text
is a QString.chars[x]
is an unsigned char. Convert it to QChar. Also convert the expression in [] to an integer.probably I shouldn't remove this line as @JonB suggested
If there's a function that seeds the generator and you want to preserve it then you can use QRandomGenerator::seed, but it's not necessary.
-
This expression
(qrand() / (qreal) RAND_MAX)
gives you a random double number in range 0..1.
So you can just replace this whole thing withQRandomGenerator::global()->generateDouble()
, which does the same. Same for the lines above it.
Or better yet - remove that whole expression with bounded() which will give you a random index in a range and you won't have to do any of those shenanigans.Now im getting this error: no operator "+=" matches these operands
text
is a QString.chars[x]
is an unsigned char. Convert it to QChar. Also convert the expression in [] to an integer.probably I shouldn't remove this line as @JonB suggested
If there's a function that seeds the generator and you want to preserve it then you can use QRandomGenerator::seed, but it's not necessary.
@Chris-Kawa thanks, changing it to
QRandomGenerator::global()->generateDouble()
fixed the widget not being show correctly.text += QChar(chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)]);
I'm getting an exception index out out of range
-
@Chris-Kawa thanks, changing it to
QRandomGenerator::global()->generateDouble()
fixed the widget not being show correctly.text += QChar(chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)]);
I'm getting an exception index out out of range
@Roberrt said in identifier qrand is undefined:
(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)
Replace this whole thing with
QRandomGenerator::global()->bounded(chars.size())