use arc4random() in qrand() if available

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2020-11-27 01:01:59 +00:00
parent 3a725cffb0
commit 079210cccb
2 changed files with 7 additions and 1 deletions

View file

@ -374,6 +374,8 @@ katie_check_function(ecvt "stdlib.h")
katie_check_function(getprogname "stdlib.h")
# FreeBSD 5.4
katie_check_function(pthread_attr_get_np "pthread_np.h" "${CMAKE_THREAD_LIBS_INIT}")
# OpenBSD 2.1
katie_check_function(arc4random "stdlib.h")
# GNU
katie_check_function(get_current_dir_name "unistd.h")
katie_check_function(prctl "sys/prctl.h")

View file

@ -1523,14 +1523,18 @@ void qsrand(uint seed)
\sa qsrand()
*/
thread_local time_t almostrandom = 0;
int qrand()
{
#if defined(QT_HAVE_ARC4RANDOM)
return ::arc4random();
#else
// Seed the PRNG once per thread with a combination of current time and its address
thread_local time_t almostrandom = 0;
if (!almostrandom) {
::time(&almostrandom);
std::srand(almostrandom + std::intptr_t(&almostrandom));
}
#endif
return std::rand();
}