正确设置随机种子以确保可重复性

正确设置随机种子以确保可重复性

本文介绍了正确设置随机种子以确保可重复性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Fortran 90子例程random_seed设置随机种子的方法非常简单.

The method for setting random seeds using the Fortran 90 subroutine random_seed is quite straightforward.

call random_seed( put=seed )

但是我找不到有关设置种子的准则的任何信息(当您需要可重复性时,这是绝对必要的).我过去听过的民俗学建议标量种子应该很大.例如. 123456789比123更好.我可以在网上找到对此的唯一支持是,建议将ifort扩展功能ran()建议使用大的奇数整数值"

But I can't find any information about guidelines for setting the seed (which is absolutely necessary when you want repeatability). Folklore I've heard in the past suggested that scalar seeds should be large. E.g. 123456789 is a better seed than 123. The only support for this I can find on the web is that it is suggested for the ifort extension function ran() that one use a "large, odd integer value"

我了解这可能是特定于实现的,并且正在使用gfortran 4.8.5,但也对ifort和(如果可能)与实现无关的一般准则感兴趣.这是一些示例代码:

I understand this might be implementation specific and am using gfortran 4.8.5 but am also interested in ifort and (if possible) general guidelines that are independent of implementation. Here's some example code:

# for compactness, assume seed size of 4, but it will depend on
# the implementation (e.g. for my version of gfortran 4.8.5 it is 12)

seed1(1:4) = [ 123456789, 987654321, 456789123, 7891234567 ]
seed2(1:4) =   123456789
seed3(1:4) = [         1,         2,         3,          4 ]

我猜想seed1很好,但是如果您手动设置(如我一样),则相当冗长,因为种子长度可以是12或33或其他任何值.而且我什至不确定这是否还好,因为我根本找不到关于设置这些种子的任何指南. IE.这些种子对于我所知道的应该是负数,或者是3位数的偶数,依此类推.尽管我猜您希望实现能对您发出警告(?).

I'd guess that seed1 is fine, but pretty verbose if you're setting it manually (as I am) as seed length can be 12 or 33 or whatever. And I'm not even sure that it's fine because I haven't been able to find any guidelines at all about setting these seeds. I.e. these seeds should be negative for all I know, or 3 digit even numbers, etc. although I guess you'd hope the implementation would warn you about that (?).

seed2seed3显然更方便设置,而且我所知道的都一样. @Ross建议seed2在这里的答案中实际上是很好的:

seed2 and seed3 are obviously more convenient to set and for all I know are just as good. @Ross suggests that seed2 is in fact fine in his answer here: Random number generator (RNG/PRNG) that returns updated value of seed

因此,我在总结中的问题只是:如何正确设置种子? seed1seed3中的任何一个或全部都可以接受吗?

So my question in summary is just: How can I correctly set the seed? Are any or all of seed1 to seed3 acceptable?

推荐答案

设置种子的准则取决于RANDOM_NUMBER使用的PRNG算法,但总的来说,您提供的熵"越多越好.

Guidelines for setting the seed depend on the PRNG algorithm that RANDOM_NUMBER uses, but in general the more "entropy" you provide the better.

如果只有一个标量值,则可以使用一些简单的PRNG将其扩展为RANDOM_SEED所需的完整种子数组.参见例如示例代码中 https://gcc中的函数lcg .gnu.org/onlinedocs/gcc-4.9.1/gfortran/RANDOM_005fSEED.html

If you have a single scalar value, you can use some simple PRNG to expand that to the full seed array required by RANDOM_SEED. See e.g. the function lcg in the example code at https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gfortran/RANDOM_005fSEED.html

当前版本的GFortran具有一些针对不良种子的保护措施,它应相对不受哑"种子的影响(例如,所有seed(:)值相同,或者所有值很小甚至为零),但可移植到其他编译器遵循我上面建议的方法可能仍然是一个好主意.

Current versions of GFortran have some protections against bad seeds, and it should be relatively immune towards "dumb" seeding (e.g. all values of seed(:) identical, or all values small or even zero), but for portability to other compilers following something like I suggested above might still be a good idea.

这篇关于正确设置随机种子以确保可重复性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:39