【问题标题】:my c++ random number is not random我的 C++ 随机数不是随机的
【发布时间】:2015-09-10 23:09:33
【问题描述】:

我正在运行这个

#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
namespace mpi = boost::mpi;

int main()
{
    mpi::environment env;
    mpi::communicator world;



    srand (time(NULL));
    std::srand(time(0) + world.rank());
    int my_number = std::rand();
    if (world.rank() == 0) {
        std::vector<int> all_numbers;
        gather(world, my_number, all_numbers, 0);
        for (int proc = 0; proc < world.size(); ++proc)
            std::cout << "Process #" << proc << " thought of "
            << all_numbers[proc] << std::endl;
    } else {
        gather(world, my_number, 0);
    }

    return 0;
}

为了分布式生成随机数,但是,它每次都会给我大约相同数量级的数字....

dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238772362
Process #1 thought of 238789169
Process #2 thought of 238805976
Process #3 thought of 238822783
dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238805976
Process #1 thought of 238822783
Process #2 thought of 238839590
Process #3 thought of 238856397
dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238856397
Process #1 thought of 238873204
Process #2 thought of 238890011
Process #3 thought of 238906818
dhcp-18-189-66-216:ising2 myname$ 

在网站上,http://www.boost.org/doc/libs/1_55_0/doc/html/mpi/tutorial.html,其他人说他们得到了:

Process #0 thought of 332199874
Process #1 thought of 20145617
Process #2 thought of 1862420122
Process #3 thought of 480422940
Process #4 thought of 1253380219
Process #5 thought of 949458815
Process #6 thought of 650073868

我很困惑....有什么帮助吗?谢谢。

【问题讨论】:

  • 为什么要播种两次?
  • 使用std::rand 是您的问题。 不要It's basically junk。使用proper random number generator
  • @ScottHunter 我用十几种不同的方法将该评论修改为一个链接。 rand() 长期以来一直被认为几乎无用。除非你真的不在乎你的数字有多随机,否则请使用适当的库。
  • 思考一下:任何始终保证从相同输入产生相同输出的算法都是确定性的。确定性算法不能输出比输入它们更多的随机性。

标签: c++ random


【解决方案1】:

我认为你的随机生成应该是这样的:

int max=100, min=0;
srand(time(NULL));
int random = (rand() % (max-min)) + min;

【讨论】:

    【解决方案2】:

    问题很可能是由您的rand 引起的。请参阅此问题的讨论和答案:First random number is always smaller than rest

    似乎相邻种子(您的情况)的生成数字可能非常相关。 rand 的实现可能会有所不同,对于某些实现来说,这似乎是一种更为明显的现象。

    【讨论】:

      【解决方案3】:

      你的问题是 cstdlib 的 rand() 函数。它不是一个好的随机数生成器。如果您想在 c++ 中使用正确的随机数,请使用 c++-11 的标头中的一些或外部随机数生成器(例如 mersenne twister)。 但是,在并行程序中使用随机数并非易事。您应该使用专门用于此的随机数生成器(例如 r250_omp.h)。

      【讨论】:

        猜你喜欢
        • 2011-11-07
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 2016-11-07
        • 1970-01-01
        相关资源
        最近更新 更多