Is sland(time(NULL)) sufficiently different in the multi-thread function?

Asked 1 years ago, Updated 1 years ago, 100 views

In C language, when random values are generated, sland(time(NULL)) is often added. As far as I know, you make a random value by giving time as a seed

Generate 8 threads simultaneously in a 4-core 8-threaded environment. This thread generates randomness all at once, and the problem is that it is feared that they are repeating the same thing too much. There may be a slight difference in time between threads, but I think it may be because the seeds of time are the same as each other because they are rotated at the same time.

If anyone has thought about the precision of giving time as a seed, please give us your opinion.

c random

2022-09-22 20:54

1 Answers

Seed values set by srand() are shared by all threads. In other words, even if srand() is called per thread, the seed set as the last call will be used by the entire program.

When threads call time(), the seed will eventually become the same, whether there is a time difference or not, so that each thread will not receive the same order of digits in the land() call.

Check Linux's /dev/random or Windows' CryptGenRandom for how all threads generate random numbers regardless of seeds.

If C++ is available, you can also use random_device.


2022-09-22 20:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.