如何为伪随机数生成器设置自定义种子 [英] How to set custom seed for pseudo-random number generator

查看:163
本文介绍了如何为伪随机数生成器设置自定义种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用randn伪随机数生成器的情况下,我需要执行一些测试.我如何自己设置种子,所以每次运行此测试时,我都会得到相同的结果? (是的,我知道这有点奇怪,但这就是问题所在).

I need to perform few tests where I use randn pseudo random number generator. How can I set the seed on my own, so every time I run this test I will get the same results? (yeah, I know it's a little bit weird, but that's the problem).

我发现具有seed属性的RANDSTREAM对象,但是它是只读的.有什么方法可以使用它为生成器播种吗?

I've found the RANDSTREAM object that has the seed property, but it's read only. Is there any way to use it for seeding the generator?

推荐答案

执行此操作的旧方法:

randn('seed',0)

新方法:

s = RandStream('mcg16807','Seed',0)
RandStream.setDefaultStream(s)

请注意,如果使用新方法,则randrandn共享同一流,因此,如果您同时调用这两个流,则与旧方法(具有单独的生成器)相比,您可能会发现生成的数字不同.因此,仍支持旧方法(和旧代码).

Note that if you use the new way, rand and randn share the same stream so if you are calling both, you may find different numbers being generated compared to the old method (which has separate generators). The old method is still supported for this reason (and legacy code).

请参见 http://www.mathworks.com/help/techdoc /math/bsn94u0-1.html 了解更多信息.

这篇关于如何为伪随机数生成器设置自定义种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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