简体   繁体   English

为什么是 <random> 每次使用std :: uniform_int_distribution时,库都会产生相同的结果

[英]Why is <random> library producing the same results every time when using std::uniform_int_distribution

Notes: 笔记:

  1. My compiler is g++ 5.1.0-2 with c++14 language standards 我的编译器是g ++ 5.1.0-2,带有c ++ 14语言标准
  2. My IDE is Code::Blocks (I tried the code on Dev-C++ too) 我的IDE是Code :: Blocks(我也尝试过Dev-C ++上的代码)
  3. The online IDEs I tested the code on were http://cpp.sh/ (C++ shell) and https://www.codechef.com/ide (CodeChef.com's IDE) both running c++14 我测试代码的在线IDE是http://cpp.sh/(C ++ shell)和https://www.codechef.com/ide(CodeChef.com的IDE)都运行c ++ 14
  4. The code runs ok when using an online IDE, which puzzles me even more. 使用在线IDE时代码运行正常,这让我更加困惑。

Here is the code: 这是代码:

#include <iostream>
#include <random>
#include <cstdlib>
#include <ctime>
#include <chrono>

int main() {
    srand(time(0));
    long long seed = rand();
    std::default_random_engine rand_num(seed);
    std::uniform_int_distribution<long long> range(0, 10);
    long long a = range(rand_num);
    long long b = rand_num();
    std::cout<<seed<<"\n"; // the seed is different every time (tested)
    std::cout<<a<<"\n";
    std::cout<<b<<"\n";
    system("pause");
    return 0;
}

The one with std::uniform_int_distribution (a) isn't random when running the code on my own computer but works ok and creates a random number on the online IDEs. 具有std :: uniform_int_distribution(a)的那个在我自己的计算机上运行代码时不是随机的,但是可以正常工作并在在线IDE上创建一个随机数。

The one without std::uniform_int_distribution (b) works ok with both online IDEs and my own computer. 没有std :: uniform_int_distribution(b)的那个适用于在线IDE和我自己的计算机。

What's the problem? 有什么问题? How can I fix it? 我该如何解决?

UPDATE: The code works ok with mt19937 engine (std::mt19937 and std::mt19937_64) 更新:代码适用于mt19937引擎(std :: mt19937和std :: mt19937_64)

It seems to be a known problem of g++ implementation on Windows. 这似乎是Windows上g ++实现的一个已知问题。 See the accepted answer to a similar question: Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1? 请参阅类似问题的已接受答案: 为什么我使用mingw gcc4.8.1使用std :: random_device进行每次运行都会得到相同的序列?

To avoid the problem you can use <chrono> facilities to seed the random number generator: 为避免此问题,您可以使用<chrono>工具为随机数生成器播种:

#include <iostream>
#include <random>
#include <cstdlib>
#include <ctime>
#include <chrono>

int main() {
    srand(time(0));
    long long seed = rand();

    std::default_random_engine rand_num{static_cast<long unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count())};
    //                                  ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
    std::uniform_int_distribution<long long> range{1,10};

    long long a = range(rand_num);
    long long b = rand_num();
    std::cout<<seed<<"\n"; // the seed is different every time (tested)
    std::cout<<a<<"\n";
    std::cout<<b<<"\n";
    system("pause");
    return 0;
}

This gave me different results at every run ( with g++ on Windows). 这给了我每次运行时的不同结果(在Windows上使用g ++)。

Your code works fine with Visual C++, but fails for me with g++ 5.2.0. 您的代码在Visual C ++中运行良好,但对于我来说,使用g ++ 5.2.0失败了。 It looks to me like a bug in gcc's standard library. 它看起来像是gcc标准库中的一个错误。

Yet more evidence that using the default_random_engine is just a bad idea in general, and using time(0) as a seed is pretty awful as well (though a quick check indicates that changing it to use random_device for the seed still produces broken results). 更多证据表明,使用default_random_engine通常只是一个坏主意,并且使用time(0)作为种子也非常糟糕(尽管快速检查表明将其更改为使用random_device进行种子仍会产生破碎的结果)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std::uniform_int_distribution 不够随机 - std::uniform_int_distribution isn't random enough 为什么不是`std::uniform_int_distribution<uint8_t> ` 和 `std::uniform_int_distribution<int8_t> ` 允许吗? - Why Aren't `std::uniform_int_distribution<uint8_t>` and `std::uniform_int_distribution<int8_t>` Allowed? 使用 c++11 std::uniform_int_distribution 生成随机素数 - Generate a random prime using c++11 std::uniform_int_distribution 为什么是 std::uniform_int_distribution<inttype> ::operator() 不是常量?</inttype> - Why is std::uniform_int_distribution<IntType>::operator() not const? 为什么 std::uniform_int_distribution 在不同系统中没有给出相同的答案? - Why does std::uniform_int_distribution not give the same answer across different systems? std::uniform_int_distribution 的分段错误 - Segmentation fault with std::uniform_int_distribution 使用 Rcpp 从 std::uniform_int_distribution 采样时出现“来自 C 堆栈溢出的段错误” - Getting "segfault from C stack overflow" when using Rcpp to sample from std::uniform_int_distribution 的std :: uniform_int_distribution <int> g ++和msvc的范围 - std::uniform_int_distribution<int> range in g++ and msvc 使用std :: uniform_int_distribution并在以后定义其范围 - Use std::uniform_int_distribution and define its range later C++ 随机 uniform_int_distribution 在所有线程中返回相同的值 - C++ random uniform_int_distribution return same values in all threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM