Artmetic Qt/C++ Generating random number using srand() and rand() Qt

Generating random number using srand() and rand() Qt


You can use random function from stdlib and fix time to maximize pseudo random operation in a short period of short time.

const int MainWindow::getNumber() const
{
qsrand( (unsigned)time(NULL) ^random());
return qrand() % max;
}

Using qsrand(QTime::currentTime().msec()) it can also be done, but in a short period of time the function will return the same value.

Leave a Reply

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.

Related Post

wzorzec adapter

Wzorzec Adapter C++/QtWzorzec Adapter C++/Qt

Zastosowanie Adapter inaczej nazywany Nakładką (ang. wrapper) to strukturalny wzorzec projektowy, którego zadaniem jest stworzenie spójnego interfejsu dla dwóch niekompatybilnych klas. Adapter przekształca interfejs jednej z klas na interfejs drugiej.