开发者

Strange same numbers

开发者 https://www.devze.com 2023-04-06 07:10 出处:网络
I use some random numbers as initial values for my \'metaheuristics optimization\' calculations. I run my same optimization program on different computers using MPI. I surprisingly obtained a lot of s

I use some random numbers as initial values for my 'metaheuristics optimization' calculations. I run my same optimization program on different computers using MPI. I surprisingly obtained a lot of same results. For example I use 40 host computers, the results have few different values. Almost 6-7 values are the same. Actually, my results can be similar but they must not be same because I give random numbers as initials to them in the beginning of program (In my above example I must get 40 different values). If I run the program repeatedly and sequentially on a same computer, it produces different results as it should be.

I suspect that this situation is caused 开发者_StackOverflow中文版by insufficient quality of random number generation. How can I solve this problem. I open other ideas, may be different things cause this problem.

P. S. I use srand( (unsigned) time(NULL) ) once in the beginning of my program for generating random-like numbers. Then, I generate my random numbers in the range of [0, 1] by using (float)rand()/32767

One example of my results which I complained:

15.42161751
19.83328438
3.43446541
23.50453377
23.50453377
3.43446541
19.83328438
23.50453377
3.43446541
7.52127457
7.52127457
23.50453377
7.52127457
7.52127457
23.50453377
19.83328438
19.83328438
19.83328438
7.52127457
15.42161751
3.43446541
19.83328438
19.83328438
15.42161751
23.50453377
23.50453377
5.29145241
19.83328438
19.83328438
19.83328438
19.83328438
7.52127457
23.50453377
3.43446541
19.83328438
23.50453377
7.52127457
3.43446541
7.52127457
5.29145241


The random number generators may be receiving the same seed value.

My suggestion is to create a hash of some unique identifier for the computer, computer name or MAC address, and xor that into the return from time().


You're correct, the default random number generator in C++ is often not very high quality. If your compiler has implemented any of C++11 you might have more choices available, see this quick reference: http://en.wikipedia.org/wiki/C%2B%2B11#Extensible_random_number_facility . If you don't have those classes available, you can find them in boost.random.

You might also consider a source of true random numbers rather than the simulated pseudo-random numbers available from a library, for example the /dev/random device file on Linux.


That's because some of your host computers are having the same time, so the srand() takes the same time and therefore the random sequence have same starting point, so of course your are getting the same random numbers. Try to to this:

srand(time(0)*my_computer_id);


The quality of the random number generator is not the problem. Even the C random number generator won't produce duplicate values like you are seeing unless you are using the same seed. The function time has resolution in seconds, so it's not surprising that if you spawn several processes, the random number generators will get the same seed. You probably wanted a function like clock, which has a higher resolution.

Using the clock as the seed has at least one other problem: it becomes impossible to get the same results twice from your code.


Use something more precise than time(NULL). I use static_cast<int64>(clock()) + time(NULL). You could also use other sources of entropy like keyboard buffer, screen buffer, memory areas, etc. Depends on the quality of randomness your application requires.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号