简体   繁体   English

C89:具有rand_r()的多线程随机值

[英]C89: Multithreaded Random Values with rand_r()

So, I was curious how someone could get random values across multiple threads with the rand_r command. 因此,我很好奇有人可以使用rand_r命令跨多个线程获取随机值。 If I used time(NULL) for the seed everyone ends up with the sam seed because the threads are created so close together. 如果我对种子使用time(NULL),那么每个人都会得到sam种子,因为创建的线程是如此紧密。 There actions are all based on that random seed, so all the threads remain identical for the remainder of my program. 所有动作都基于该随机种子,因此在我程序的其余部分,所有线程都保持相同。 How would one get [ USING RAND_R(*int seed) ] a random number that differed across all threads? 一个如何在所有线程中使用[USING RAND_R(* int seed)]一个不同的随机数?

Thanks in advance. 提前致谢。

The man page for rand_r on Ubuntu Linux 14.04 suggests: 在Ubuntu Linux 14.04上rand_r的手册页建议:

The value pointed to by the seedp argument of rand_r() provides only a very small amount of state, so this function will be a weak pseudo-random generator. rand_r()seedp参数所指向的值仅提供非常少量的状态,因此该函数将是一个弱伪伪生成器。 Try drand48_r(3) instead. 尝试改用drand48_r(3)

您可以使用arc4random为每个线程生成种子值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM