久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從多個線程使用 stdlib 的 rand()

Using stdlib#39;s rand() from multiple threads(從多個線程使用 stdlib 的 rand())
本文介紹了從多個線程使用 stdlib 的 rand()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有幾個線程都運行相同的功能.在其中的每一個中,它們都會多次生成不同的隨機(jī)數(shù).我們試圖通過將 srand(time(0)) 放在函數(shù)的開頭來做到這一點,但它們似乎都得到了相同的數(shù)字.

I have several threads which all run the same function. In each of these they generate a different random number several times. We tried to do this by putting srand(time(0)) at the start of the function, but it seems that they all get the same number.

我們是否需要每個程序只調(diào)用一次 srand(time(0)) ,即在 main 的開頭(例如),在每個程序的開頭被多次調(diào)用的函數(shù),還是別的什么?

Do we need to call srand(time(0)) only once per program, i.e at the start of main (for example), at the start of each function that is called several times, or something else?

推薦答案

srand() 種子隨機(jī)數(shù)生成器.您應(yīng)該只需要在啟動期間調(diào)用 srand(time(NULL)) 一次.

srand() seeds the random number generator. You should only have to call srand(time(NULL)) once during startup.

也就是說,文檔指出:

函數(shù) rand()不可重入的或線程安全,因為它使用隱藏在每次調(diào)用時修改的狀態(tài).這可能只是種子值被下一次調(diào)用使用,或者它可能做一些更精細(xì)的事情.為了獲得可重現(xiàn)的行為線程應(yīng)用程序,此狀態(tài)必須明確.功能rand_r() 提供了一個指向一個 unsigned int,用作狀態(tài).這是一個非常少量的狀態(tài),所以這個功能會很弱偽隨機(jī)發(fā)生器.嘗試drand48_r(3) 代替.

The function rand() is not reentrant or thread-safe, since it uses hidden state that is modified on each call. This might just be the seed value to be used by the next call, or it might be something more elaborate. In order to get reproducible behaviour in a threaded application, this state must be made explicit. The function rand_r() is supplied with a pointer to an unsigned int, to be used as state. This is a very small amount of state, so this function will be a weak pseudo-random generator. Try drand48_r(3) instead.

上面強(qiáng)調(diào)的部分可能是你所有線程得到相同數(shù)字的原因.

The emphasized part of the above is probably the reason why all your threads get the same number.

這篇關(guān)于從多個線程使用 stdlib 的 rand()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉(zhuǎn)一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進(jìn)行排序)
主站蜘蛛池模板: 国产精品免费在线播放 | 伊人成人在线视频 | 天天干天天弄 | 亚洲高清在线播放 | 成人在线免费观看网站 | 色综合久久88色综合天天 | 黄色片aaa| 国产福利一区二区三区 | 国产综合视频 | 欧美三级又粗又硬 | 金银在线| 夜夜操夜夜爽 | 亚洲激情久久 | 亚洲爱爱网 | 精品视频在线播放 | 一级黄色录像片 | 人人干人人爱 | 国产精品区二区三区日本 | 亚洲成人中文字幕 | 国产成人影视 | 一级片日韩 | 成人免费在线观看网站 | 天天视频黄 | 日本精品视频 | 日韩专区在线观看 | av片在线免费观看 | 成人在线视频网站 | 日韩中文字幕在线视频 | www..com黄| 亚洲在线观看视频 | 亚洲免费在线 | 久色91| 欧美成人精品激情在线观看 | 三级网站在线 | 丁香九月婷婷 | 在线观看欧美日韩 | 午夜av网站 | 欧美顶级黄色大片免费 | 欧美日韩精品久久久免费观看 | 国产无遮挡又黄又爽免费网站 | 免费成人黄色 |