Дмитрий
Dec. 31, 2020, 4:19 p.m.

Generating pseudo-random numbers in Qt (QRandomGenerator)

This note is a small addition to the article Generating pseudo-random numbers using the random STD library . The author omitted one of the useful functions of the QRandomGenerator class in it. The generator implementation is as follows:

QRandomGenerator *rg = QRandomGenerator::global();
for(int i = 0; i < 10; i++) {
    qDebug() << rg->bounded(1, 10);
}

The global() function returns a pointer to a ready-to-run random number generator. An alternative to it is the system () function, which implements the possibilities for generating random numbers embedded in the operating system. The bounded(a, b) function returns a random number from a to b. The following functions are also available to us: generateDouble() - returns one random number from the range from 0 to 1, generate() and generate64() - return a random 32 and 64 bit random number, respectively. All of the listed functions will return a new sequence of numbers each time they are run/called.
In my opinion, such a toolkit is able to cover all the needs of an average programmer for generating random numbers without connecting third-party libraries.

Recommended articles on this topic

By article asked0question(s)

2

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 24, 2025, 12:04 p.m.
    UPD: Переписал логику воспроизведения через стороннюю библиотеку BASS. Там выбрать можно
  • Evgenii Legotckoi
    April 16, 2025, 5:08 p.m.
    Благодарю за отзыв. И вам желаю всяческих успехов!
  • IscanderChe
    April 12, 2025, 5:12 p.m.
    Добрый день. Спасибо Вам за этот проект и отдельно за ответы на форуме, которые мне очень помогли в некоммерческих пет-проектах. Профессиональным программистом я так и не стал, но узнал мно…
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…