What numbers that I can put in numpy.random.seed()?
我注意到您可以在
考虑一个非常基本的随机数生成器:
1 | Z[i] = (a*Z[i-1] + c) % m |
在这里,
您可能已经注意到,要开始此生成过程-要具有
将种子的初始值确定为7,以开始该过程。但是,该值不用于生成随机数。相反,它用于生成第一个
伪随机数生成器的最重要特征是其不可预测性。通常,只要您不共享种子,就可以使用所有种子,因为今天的生成器要比这复杂得多。但是,作为下一步,您也可以随机生成种子。您可以跳过第一个
主要资料来源:Law,A. M.(2007)。仿真建模和分析。塔塔·麦格劳·希尔(Tata McGraw-Hill)。
实际上,通常称为"随机数序列"的是"伪随机"数序列,因为这些值是使用确定性算法计算的,而概率没有实际作用。
"种子"是序列的起点,并且保证的是,如果您从同一种子开始,则将获得相同的数字序列。例如,这对于调试非常有用(当您在程序中查找错误时,您需要能够重现该问题并进行研究,一个不确定的程序将很难调试,因为每次运行都会不同) 。
简短的答案:
-
不使用参数或使用
None -RNG通过操作系统的随机数生成器(通常是加密随机数)初始化自身 -
使用一些32位整数N-RNG将使用它基于确定性函数(相同种子相同状态)初始化其状态
-
使用类似数组的32位整数n 0 sub>,n 1 sub>,n 2 sub>等序列-再次,RNG将使用它基于确定性函数(种子相同状态的相同值)初始化其状态。尽管源代码中有魔术数字,但尚不清楚它们为什么要执行自己的操作,但这是通过某种哈希函数来完成的。
如果要执行可重复且简单的操作,请使用单个整数。
如果您想做一些可重复的事情,但第三方不太可能猜到,请使用包含某些32位整数序列的元组或列表或numpy数组。例如,您可以使用
更长的答案
-
通过查看
numpy.random.RandomState 的文档,其中numpy.random 使用numpy.random.* 函数的实例(但是您也可以使用孤立的独立实例) -
查看mtrand.pyx中的源代码,该源代码使用Pyrex来包装快速的C实现,以及randomkit.c和initarray.c。
无论如何,这就是
Compatibility Guarantee A fixed seed and a fixed series of calls to
RandomState methods using the same parameters will always produce the same results up to roundoff error except when the values were incorrect. Incorrect values will be fixed and the NumPy version in which the fix was made will be noted in the relevant docstring. Extension of existing parameter ranges and the addition of new parameters is allowed as long the previous behavior remains unchanged.Parameters:
seed : {None, int, array_like}, optionalRandom seed used to initialize the pseudo-random number generator. Can be any integer between 0 and 2**32 - 1 inclusive, an array (or other sequence) of such integers, or
None (the default). If seed isNone , then RandomState will try to read data from/dev/urandom (or the Windows analogue) if available or seed from the clock otherwise.
它没有说明种子的使用方式,但是如果您深入研究源代码,它指的是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def seed(self, seed=None): cdef rk_error errcode cdef ndarray obj"arrayObject_obj" try: if seed is None: with self.lock: errcode = rk_randomseed(self.internal_state) else: idx = operator.index(seed) if idx > int(2**32 - 1) or idx < 0: raise ValueError("Seed must be between 0 and 2**32 - 1") with self.lock: rk_seed(idx, self.internal_state) except TypeError: obj = np.asarray(seed).astype(np.int64, casting='safe') if ((obj > int(2**32 - 1)) | (obj < 0)).any(): raise ValueError("Seed must be between 0 and 2**32 - 1") obj = obj.astype('L', casting='unsafe') with self.lock: init_by_array(self.internal_state, <unsigned long *>PyArray_DATA(obj), PyArray_DIM(obj, 0)) |
这是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | extern void init_by_array(rk_state *self, unsigned long init_key[], npy_intp key_length) { /* was signed in the original code. RDH 12/16/2002 */ npy_intp i = 1; npy_intp j = 0; unsigned long *mt = self->key; npy_intp k; init_genrand(self, 19650218UL); k = (RK_STATE_LEN > key_length ? RK_STATE_LEN : key_length); for (; k; k--) { /* non linear */ mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) + init_key[j] + j; /* for > 32 bit machines */ mt[i] &= 0xffffffffUL; i++; j++; if (i >= RK_STATE_LEN) { mt[0] = mt[RK_STATE_LEN - 1]; i = 1; } if (j >= key_length) { j = 0; } } for (k = RK_STATE_LEN - 1; k; k--) { mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i; /* non linear */ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ i++; if (i >= RK_STATE_LEN) { mt[0] = mt[RK_STATE_LEN - 1]; i = 1; } } mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ self->gauss = 0; self->has_gauss = 0; self->has_binomial = 0; } |
这实质上是使用提供的种子值序列中的每个值,以非线性的,类似于哈希的方法"消除"随机数状态。
要了解随机种子的含义,您首先需要了解"伪随机"数字序列,因为这些值是使用确定性算法计算的。
因此,您可以将此数字视为一个起始值,以计算从随机生成器获得的下一个数字。在此处放置相同的值将使您的程序每次都获得相同的"随机"值,因此您的程序将具有确定性。
如这篇文章所说
they (
numpy.random andrandom.random ) both use the Mersenne twister sequence to generate their random numbers, and they're both completely deterministic - that is, if you know a few key bits of information, it's possible to predict with absolute certainty what number will come next.
如果您真的在乎随机性,请要求用户产生一些噪音(一些随意的话),或者只是将系统时间作为种子。
如果您的代码在Intel CPU(或具有最新芯片的AMD)上运行,我也建议您检查RdRand软件包,该软件包使用cpu指令
参考文献:
基本上,数字每次都保证相同的"随机性"。
更恰当地,数字是种子,可以是整数,任意长度的整数的数组(或其他序列)或默认值(无)。如果没有种子,那么random将尝试从/ dev / urandom读取数据(如果可用),否则从时钟生成种子。
编辑:最诚实的说,只要您的程序不是需要超级安全的程序,选择什么都不重要。在这种情况下,请不要使用这些方法-如果需要加密安全的伪随机数生成器,请使用
这里要理解的最重要概念是伪随机性。一旦了解了这个想法,就可以确定您的程序是否真的需要种子等。我建议您在这里阅读。