简体   繁体   English

我的c ++随机数不是随机的

[英]my c++ random number is not random

I am running this 我正在运行这个

#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;
}

to distributively generate random number, however, it gives me the number around the same magnitude everytime.... 然而,为了分配地生成随机数,它每次都给出了相同大小的数字....

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$ 

In the website, http://www.boost.org/doc/libs/1_55_0/doc/html/mpi/tutorial.html , others said they get: 在网站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

I am very confused.... Any help? 我很困惑....有什么帮助吗? Thank you. 谢谢。

You Problem is the rand() function of the cstdlib. You Problem是cstdlib的rand()函数。 It is no good random number generator. 它不是一个好的随机数发生器。 If you want to use proper random numbers in c++ use some from the header from c++-11 or external random number generators (eg mersenne twister). 如果你想在c ++中使用适当的随机数,可以使用c ++ - 11或外部随机数生成器(例如mersenne twister)中的一些。 But nonetheless, using random numbers in for parallel programs is no easy task. 但是,在并行程序中使用随机数并非易事。 You should use random number generators, which are specialised on that (eg r250_omp.h). 您应该使用随机数生成器,它们是专门的(例如r250_omp.h)。

The problem is likely to be caused by your rand . 问题很可能是你的rand造成的。 See the discussion and answers for this question: First random number is always smaller than rest 请参阅此问题的讨论和答案: 第一个随机数总是小于休息

It seems that the generated numbers for neighboring seeds (your case) can be quite heavily correlated. 似乎相邻种子(您的情况)生成的数字可能非常相关。 rand's implementations might vary, and for some implementations it seems to be a much more pronounced phenomena. 兰德的实现可能会有所不同,对于某些实现,它似乎是一个更加明显的现象。

I think your random gen should be like this: 我认为你的随机类应该是这样的:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM