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

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

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

問題描述

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

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.

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

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() 種子隨機數生成器.您應該只需要在啟動期間調用 srand(time(NULL)) 一次.

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

也就是說,文檔指出:

函數 rand()不可重入的或線程安全,因為它使用隱藏在每次調用時修改的狀態.這可能只是種子值被下一次調用使用,或者它可能做一些更精細的事情.為了獲得可重現的行為線程應用程序,此狀態必須明確.功能rand_r() 提供了一個指向一個 unsigned int,用作狀態.這是一個非常少量的狀態,所以這個功能會很弱偽隨機發生器.嘗試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.

上面強調的部分可能是你所有線程得到相同數字的原因.

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

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

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 在线视频成人 | 精品影院 | 欧美日韩高清免费 | 国产精品久久久一区二区三区 | 在线色网 | 亚洲欧美久久 | 91精品国产91久久久久久最新 | 精品久久伊人 | 日本精a在线观看 | 久久这里只有 | 国产在线精品一区二区三区 | 日韩区 | 欧美精品一区二区三区在线 | 欧洲妇女成人淫片aaa视频 | 国产一区不卡 | 天天操天天摸天天干 | 欧美又大粗又爽又黄大片视频 | 国产一区二区三区在线观看免费 | 99爱在线视频 | 国产精品成人av | 久久久久九九九女人毛片 | 91欧美精品成人综合在线观看 | 青青久久久 | 亚洲精品久久 | 91综合网| 国产精品一区在线播放 | 99精品国产一区二区三区 | 国产精品国产三级国产aⅴ无密码 | 国产精品久久久久久久久 | 国产精品成人一区 | 毛片区 | 国产1区2区| 国产精品精品久久久 | 久久一区二区免费视频 | 91九色在线观看 | 亚洲视频精品在线 | 性色视频 | 99re热这里只有精品视频 | 99久久亚洲 | 国产精品完整版 | aaaaaa大片免费看最大的 |