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

查看:30
本文介绍了具有多个 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.

因此,为了保证最佳的统计特性,最好使用支持子流的生成器.不幸的是,目前支持此属性.与默认的 Mersenne Twister 生成器 ('mt19937ar') 相比,它们的周期要小得多.以下是创建和使用带有子流的随机数流的方法:

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 算法,例如,如果您计划进行大规模的 Monte Carlo 模拟,这可能很重要.如果您打算使用随机数(和并行化),我建议您花一些时间阅读 RandStream 并通过 示例在这里.

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天全站免登陆