What difference between rand() and random() functions?(rand() 和 random() 函数有什么区别?)
问题描述
有一次,我的老师教我使用 randomize()
和 random()
函数在 C++ Builder 中生成伪随机数.现在我更喜欢在 VS 2012 中工作,但是当我尝试在 VS 2012 中使用这些函数时,即使我添加了 #include
,它也会显示未找到标识符".经过一段时间的谷歌搜索,我发现还有 rand()
和 srand()
函数.它们有什么区别,哪个更好用?
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()
不是标准库的一部分.也许您的老师用这些名称编写了用于您班级的函数,或者您的意思可能是 random()
和 srandom()
,它们是 POSIX 的一部分,在 Windows 上不可用.rand()
和 srand()
是标准库的一部分,将由任何符合标准的 C++ 实现提供.
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()
通常是低质量的 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()
的结果转化为可以直接使用的数字,通常依赖于难读易出错的代码,而使用
分发很容易并且产生可读的代码.
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()
在给定分布中生成值的常用方法进一步降低了生成数据的质量.%
通常会使数据产生偏差,浮点除法仍然会产生非均匀分布.
分布质量更高,可读性更强.
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()
不是线程安全的.一些实现可以保证线程安全,但这不是必需的标准.<random>
提供的引擎将 pRNG 状态封装为具有值语义的对象,允许对状态进行灵活控制.
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()
只允许有限范围的种子.
中的引擎可以使用允许最大可能种子数据的种子序列进行初始化.seed_seq
还实现了一个常见的 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() 函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:rand() 和 random() 函数有什么区别?
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01