- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是否保证 random_device 不会在每个新线程的相同内部状态下启动?那么下面的代码很可能给出两个不同的值?
#include <iostream>
#include <random>
#include <thread>
#include <mutex>
using namespace std;
int main()
{
auto thr = []()
{
static mutex mtx;
mtx.lock();
cout << random_device()() << " " << endl;
mtx.unlock();
};
thread t1( thr );
thread t2( thr );
t1.join();
t2.join();
}
最佳答案
没有这样的保证。
在 cppreference 上我们可以读取
std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.
这基本上取决于实现。
另一件事是创建新的 random_device
会产生性能成本。最好重复使用同一个。
auto thr = []()
{
static mutex mtx;
static random_device rd{};
mtx.lock();
cout << rd() << " " << endl;
mtx.unlock();
};
关于c++ - 每个线程的 random_device 是否以不同的状态启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352384/
我有一个包含两个随机源的类。 std::random_device rd; std::mt19937 random_engine; 我通过调用 std::random_device 为 std::mt
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我需要从不同的 C++ 随机数生成算法中获取数据,为此我创建了一些程序。其中一些使用伪随机数生成器,而另一些使用 random_device(非确定性随机数生成器)。下面的程序属于第二组: #incl
我对 c++11 随机库有点困惑。 我的理解:我们需要两个独立的概念: 随机引擎,可以是: 伪(需要种子)又名 PRNG 真正的随机数生成器 分布:它将从引擎获得的数字映射到特定的区间,使用特定的分布
是否保证 random_device 不会在每个新线程的相同内部状态下启动?那么下面的代码很可能给出两个不同的值? #include #include #include #include us
#include #include using namespace std; int main() { vector coll{1, 2, 3, 4}; shuffle(coll.
我看到很多人一起谈论安全和 std::random_device。 例如,here幻灯片 22。 根据 cppreference , std::random_device : std::random_
根据标准,std::random_device 按以下方式工作: result_type operator()(); Returns: A non-deterministic random value
典型的现代获取随机数的方法是这样的: std::random_device rd; std::mt19937 engine{rd()}; std::uniform_int_distribution<>
我有一些看起来有点像这样的代码: std::random_device rd; #pragma omp parallel { std::mt19937 gen(rd()); #prag
我试着解决这个问题: c++0x_warning.h:32: Fehler:#error This file requires compiler and library support for the
有人知道如何在 ubuntu 上安装 random_device 吗?安装了来自 repo 的所有 boost 包。 我收到的错误是: fatal error :boost/random/random
我尝试运行一个来自 cppreference 的简单示例关于std::random_device , 但在函数调用线上 d(rd1)程序进入无限循环,永不返回。 代码如下: #include #in
我正在使用 std::random_device 并想检查它的剩余熵。根据 cppreference.com: std::random_device::entropy double entropy()
我是 C++ 初学者,我对 C++0x 随机数生成器有疑问。我想使用 Mersenne twister 引擎来生成随机 int64_t 数字,并且我使用我之前找到的一些信息编写了一个函数: #incl
假设我有这个跨平台程序 #include #include int main() { std::random_device rd; std::cout dist(0, 9);
std::random_device 的拷贝构造函数被删除了,我不知道为什么。 我从 docs 中找到的唯一笔记是: 2) The copy constructor is deleted: std::
引自cppreference : std::random_device is a non-deterministic random number engine, although implementa
gcc implementation std::random_device 对我来说似乎很奇怪。具体来说,第 137 行: random_device::result_type random_
我在 Windows 中使用 g++ 和 MinGW 来编译我的 c++ 代码,它看起来像这样: std::mt19937_64 rng(std::random_device{}()); std::u
我是一名优秀的程序员,十分优秀!