I need to generate a random value in Qt, I was using qrand() but I realize that the numbers generated was not random totally, because even though the first one was random, the rest follow the same patter. Example, I want a random with 9 as maximum number every time I press a QPushButton, the numbers generated the first time I run the program was: 5,8,7,4,8,1,3. But when I run it again the numbers it shows were 5,8,7,4,8,1,3 again, obviously pressing the same times the button. What is the best way to generate random?
Add my code:
//ID of Persona should be random, that's why I generate it as ID
struct Persona
{
int ID;
Persona()
{
ID = generateID();
}
int generateID();
};
//This generates the random
int Persona::generateID()
{
int max = 9;
int random = qrand() %max;
return random;
}
void MainWindow::on_pushButton_clicked()
{
Persona *nueva = new Persona();
int random = nueva->ID;
qDebug()<<random;
}
Aucun commentaire:
Enregistrer un commentaire