久久久久久久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 排序功能對列表進行排序)
主站蜘蛛池模板: 精品麻豆剧传媒av国产九九九 | 中文天堂在线一区 | 99久久久国产精品 | 一区二区三区欧美在线 | 国产精品久久久久久久免费大片 | 99久久久国产精品 | 久久爆操| 成人免费看| 中文字幕精品视频 | 日韩无 | www国产亚洲精品久久网站 | 国产午夜精品一区二区三区四区 | 午夜久久久久久久久久一区二区 | 三级在线免费观看 | 一区二区在线不卡 | 国内精品视频在线 | 九九久久久久久 | 亚洲成年在线 | 欧美理论片在线观看 | 91影院在线观看 | japan25hdxxxx日本 做a的各种视频 | 国产一区二区三区四区三区四 | 欧美日韩亚洲视频 | 福利视频一区二区三区 | 91久久精品一区二区三区 | 欧美人妇做爰xxxⅹ性高电影 | 亚洲一区精品在线 | 精品视频在线播放 | www.日韩 | www免费视频 | 69电影网| 久久久国产一区 | 午夜极品| 国产精品自拍视频 | 成人免费观看网站 | 一级做a | 亚洲 中文 欧美 日韩 在线观看 | 国产91网址 | 在线高清免费观看视频 | 一区二区三区国产好 | 国产91在线视频 |