具有多个CPU的随机数生成器Matlab [英] Random Number Generator Matlab with Multiple CPUs

查看:143
本文介绍了具有多个CPU的随机数生成器Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个使用多个CPU并行运行的matlab脚本.然后,脚本应打印出一系列正态分布的随机数.目前,我的脚本如下所示:

I would like to write a matlab script which runs in parallel using multiple CPUS. The script should then print out a sequence of normally distributed random numbers. At the moment my script looks like this:

matlabpool close force local
clusterObj = parcluster;
matlabpool(clusterObj);

parfor K = 1:10
    disp(randn)
end

它按预期打印出一个随机数序列.但是,当我再次运行代码时,它将再次打印出完全相同的数字序列.我不想这样.每次我运行脚本时,都应该打印出一个独立的随机数字序列.同样,每次启动matlab时,我的脚本在首次运行时都应打印出10个随机生成的数字的不同序列.我该怎么做呢?

It prints out a sequence of random numbers as expected. However, when I run the code again it, once again, prints out that exact same sequence of numbers. I do not want this. Each time I run my script it should print out an independently random sequence of numbers. Similarly, each time I start matlab, my script should, when I run it for the first time, print out a different sequence of 10 randomly generated numbers. How do I do this?

推荐答案

到目前为止提供的解决方案实际上是不正确的,甚至可能是个坏主意.一个人应该避免重复设置生成器的种子.更重要的是,使用不同种子分别创建的两个流不一定是独立的.在此页面上进行了介绍,该页面描述了多个流的创建:

The solutions given so far are really not correct and may even be bad ideas. One should avoid setting the seed of the generator repeatedly. More importantly, two streams created separately with different seeds are not necessarily independent. This is addressed on this page that describes the creation of multiple streams:

对于不显式支持独立流的生成器类型,不同的种子提供了一种创建多个流的方法.但是,使用为多个独立流专门设计的生成器是更好的选择,因为可以更好地理解跨流的统计属性.

For generator types that do not explicitly support independent streams, different seeds provide a method to create multiple streams. However, using a generator specifically designed for multiple independent streams is a better option, as the statistical properties across streams are better understood.

因此,为了保证最佳的统计特性,最好使用支持子流的生成器.不幸的是,目前只有乘法滞后的斐波那契生成器('mlfg6331_64')和组合的多个递归生成器('mrg32k3a')

Thus, to guarantee the best statistical properties it is best to use a generator that supports substreams. Unfortunately, only the multiplicative lagged Fibonacci generator ('mlfg6331_64') and combined multiple recursive generator ('mrg32k3a') currently support this property. Compared to the default Mersenne Twister generator ('mt19937ar') these have significantly smaller periods. Here is how you would go about creating and using a random number stream with substreams:

seed = 1;
n = 10;
[stream{1:n}] = RandStream.create('mrg32k3a','NumStreams',n,'Seed',seed);
parfor k = 1:n
    r = randn(stream{k},[1 3]);
    disp(r);
end

几件事.仅在循环外的一个调用中生成所有随机数,您可能会获得更好的性能.这也将允许您使用默认的Mersenne Twister算法,例如,如果您计划进行大规模的蒙特卡洛模拟,这可能很重要.如果要使用随机数(和并行化),建议您花一些时间阅读

Several things. You may get much better performance simply generating all of your random numbers in one call outside of your loop. This will also allow you to use the default Mersenne Twister algorithm, which may be important if, for example, you plan on doing large-scale Monte Carlo simulations. If you're going to be working with random numbers (and parallelization) I recommend that you spend some time reading the documentation for the RandStream class and going through the examples here.

这篇关于具有多个CPU的随机数生成器Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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