問題描述
有一次,我的老師教我使用 randomize()
和 random()
函數(shù)在 C++ Builder 中生成偽隨機數(shù).現(xiàn)在我更喜歡在 VS 2012 中工作,但是當我嘗試在 VS 2012 中使用這些函數(shù)時,即使我添加了 #include
,它也會顯示未找到標識符".經(jīng)過一段時間的谷歌搜索,我發(fā)現(xiàn)還有 rand()
和 srand()
函數(shù).它們有什么區(qū)別,哪個更好用?
Once, my teacher taught me to use randomize()
and random()
function for generating pseudorandom numbers in C++ Builder. Now I prefer working in VS 2012, but when I tried to use these functions there it says that "identifier not found", even when I added #include <stdlib.h>
. After some time of Googling I found that there are also rand()
and srand()
functions. What is the difference between them and which is it better to use?
推薦答案
randomize()
和 random()
不是標準庫的一部分.也許您的老師用這些名稱編寫了用于您班級的函數(shù),或者您的意思可能是 random()
和 srandom()
,它們是 POSIX 的一部分,在 Windows 上不可用.rand()
和 srand()
是標準庫的一部分,將由任何符合標準的 C++ 實現(xiàn)提供.
randomize()
and random()
are not part of the standard library. Perhaps your teacher wrote functions with these names for use in your class, or maybe you really mean random()
and srandom()
which are part of POSIX and not available on Windows. rand()
and srand()
are part of the standard library and will be provided by any standard conforming implementation of C++.
您應該避免使用 rand()
和 srand()
并使用新的 C++11
庫.
是作為 C++11 標準的一部分添加的(VS2012 確實提供了它).
You should avoid rand()
and srand()
and use the new C++11 <random>
library. <random>
was added as part of the C++11 standard (and VS2012 does provide it).
解釋原因的視頻:rand()
認為有害
Video explaining why: rand()
Considered Harmful
rand()
通常是低質(zhì)量的 pRNG,不適合需要合理水平不可預測性的應用程序.
提供了各種具有不同特性的引擎,適用于許多不同的用例.
rand()
is typically a low quality pRNG and not suitable for applications that need a reasonable level of unpredictability.<random>
provides a variety of engines with different characteristics suitable for many different use cases.
將rand()
的結(jié)果轉(zhuǎn)化為可以直接使用的數(shù)字,通常依賴于難讀易出錯的代碼,而使用
分發(fā)很容易并且產(chǎn)生可讀的代碼.
Converting the results of rand()
into a number you can use directly usually relies on code that is difficult to read and easy to get wrong, whereas using <random>
distributions is easy and produces readable code.
使用 rand()
在給定分布中生成值的常用方法進一步降低了生成數(shù)據(jù)的質(zhì)量.%
通常會使數(shù)據(jù)產(chǎn)生偏差,浮點除法仍然會產(chǎn)生非均勻分布.
分布質(zhì)量更高,可讀性更強.
The common methods of generating values in a given distribution using rand()
further decrease the quality of the generated data. %
generally biases the data and floating point division still produces non-uniform distributions. <random>
distributions are higher quality as well as more readable.
rand()
依賴于一個隱藏的全局資源.除其他問題外,這會導致 rand()
不是線程安全的.一些實現(xiàn)可以保證線程安全,但這不是必需的標準.<random>
提供的引擎將 pRNG 狀態(tài)封裝為具有值語義的對象,允許對狀態(tài)進行靈活控制.
rand()
relies on a hidden global resource. Among other issues this causes rand()
to not be thread safe. Some implementations make thread safety guarantees, but this is not required standard. Engines provided by <random>
encapsulate pRNG state as objects with value semantics, allowing flexible control over the state.
srand()
只允許有限范圍的種子.
中的引擎可以使用允許最大可能種子數(shù)據(jù)的種子序列進行初始化.seed_seq
還實現(xiàn)了一個常見的 pRNG 預熱.
srand()
only permits a limited range of seeds. Engines in <random>
can be initialized using seed sequences which permit the maximum possible seed data. seed_seq
also implements a common pRNG warm-up.
使用
的例子:
#include <iostream>
#include <random>
int main() {
// create source of randomness, and initialize it with non-deterministic seed
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng{seed};
// a distribution that takes randomness and produces values in specified range
std::uniform_int_distribution<> dist(1,6);
for (int i=0; i<100; ++i) {
std::cout << dist(eng) << '
';
}
}
這篇關于rand() 和 random() 函數(shù)有什么區(qū)別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!