随机数生成器函数 ran2 数值配方
假设我们以负整数 [种子] 开始调用函数 ran2 ,它将产生一系列随机数。如果使用相同的种子,则可以准确地重新生成序列。
现在我的问题是,有没有一种方法可以让我们直接进入序列中的某个点,然后从该点开始继续?例如,如果某个种子的随机数为 0.35、0.32、0.44、0.32、0.66、0.32、0.45。
如果我们知道产生该序列的种子,是否有办法让函数返回 0.66,然后从该点继续?
我想使用它的方式是在模拟中。如果我的模拟在某个点结束并且我需要重新启动它,我应该继续使用相同的随机数序列。 谢谢。
Given that we start the call to the function ran2 with a negative integer [the seed] it will produce a series of random numbers. The sequence can be regenerated exactly if the same seed is used.
Now my question is that is there a way that we can directly enter into some point in the sequence and then continue from that point onwards ? For example if the random numbers for a certain seed are 0.35, 0.32, 0.44,0.32,0.66,0.32, 0.45.
If we know that seed which gave rise to this sequence, is there a way to get the function to return 0.66 and then continue from that point onwards?
The way I want to use it is in a simulation. Whereby if my simulation ends at a certain point and I need to restart it I should continue with the same sequence of random numbers.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意@dirkgently 上面的评论。虽然我具体不熟悉
ran2
,但基于 此示例代码看起来有一些有状态的部分(idum
、idum2
、iy
和iv
),可以封装并存储。当您“暂停”模拟时,您可以保存此状态并在下次启动时将其写回到ran2
引擎中。恢复状态信息应该使机器回到上次停止的位置,并且随机序列可以恢复。这可能需要对ran2
代码本身进行一些修改,但应该不是一个难以折叠的更改。I'd agree with @dirkgently's comment above. While I'm not familiar with
ran2
specifically, based on this sample code it looks like there are a handful of stateful pieces (idum
,idum2
,iy
andiv
) that could be encapsulated out and stored. When you 'pause' your simulation you can save off this state and write it back into theran2
engine the next time you start things up. Restoring the state information should put the machine right back where it left off, and the random sequence can resume. This may require a little modification of theran2
code itself, but should not be a difficult change to fold in.