为什么是< random>库每次使用std :: uniform_int_distribution时产生相同的结果 [英] Why is <random> library producing the same results every time when using std::uniform_int_distribution

查看:685
本文介绍了为什么是< random>库每次使用std :: uniform_int_distribution时产生相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:


  1. 我的编译器是g ++ 5.1.0-2,使用c ++ 14语言标准

  2. 我的IDE是Code :: Blocks(我也尝试过Dev-C ++的代码)

  3. 我测试的代码是在线IDE, http://cpp.sh/ (C + + shell)和 https://www.codechef.com/ide (CodeChef.com的IDE)都运行c ++ 14

  4. 使用在线IDE时代码运行正常,


  5. 这是代码:

      #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>范围(0,10);
    long long a = range(rand_num);
    long long b = rand_num();
    std :: cout<< seed<<\\\
    ; //种子每次不同(测试)
    std :: cout<< a<<\\\
    ;
    std :: cout<< b<<\\\
    ;
    system(pause);
    return 0;
    }

    运行std :: uniform_int_distribution(a)



    没有std :: uniform_int_distribution(b)的代码在两个在线IDE上都可以正常工作和我自己的电脑。



    有什么问题?

    更新:该代码适用于mt19937引擎(std :: mt19937和std :: mt19937_64)

    解决方案

    这似乎是Windows上的g ++实现的一个已知问题。查看接受的类似问题的答案:为什么我得到相同的序列为styr :: random_device与mingw gcc4.8.1



    为了避免这个问题,你可以使用< 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>范围{1,10};

    long long a = range(rand_num);
    long long b = rand_num();
    std :: cout<< seed<<\\\
    ; //种子每次不同(测试)
    std :: cout<< a<<\\\
    ;
    std :: cout<< b<<\\\
    ;
    system(pause);
    return 0;
    }



    这在每次运行时都有不同的结果(在Windows上使用g ++) / p>

    Notes:

    1. My compiler is g++ 5.1.0-2 with c++14 language standards
    2. My IDE is Code::Blocks (I tried the code on Dev-C++ too)
    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
    4. The code runs ok when using an online IDE, which puzzles me even more.

    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.

    The one without std::uniform_int_distribution (b) works ok with both online IDEs and my own computer.

    What's the problem? How can I fix it?

    UPDATE: The code works ok with mt19937 engine (std::mt19937 and std::mt19937_64)

    解决方案

    It seems to be a known problem of g++ implementation on Windows. See the accepted answer to a similar question: why do I get same sequence for everyrun with std::random_device with mingw gcc4.8.1

    To avoid the problem you can use <chrono> facilities to seed the random number generator:

    #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).

    这篇关于为什么是&lt; random&gt;库每次使用std :: uniform_int_distribution时产生相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆