分布不影响种子。详情如下:
我检查了源代码: numpy/random/_generator.pyx#L669
如果p
给出,它将rng.random
用于获取随机值:
import numpy
pop = numpy.arange(20)
seed = 1
rng = numpy.random.default_rng(seed)
# rng.choice works like bellow
rand = rng.random()
p = numpy.repeat(1/len(pop),len(pop))
cdf = p.cumsum()
cdf /= cdf[-1]
uniform_samples = rand
idx = cdf.searchsorted(uniform_samples, side='right')
idx = numpy.array(idx, copy=False, dtype=numpy.int64) # yields 10
print(idx)
# -----------------------
rng = numpy.random.default_rng(seed)
idx = rng.choice(pop,p=numpy.repeat(1/len(pop),len(pop))) # same as above
print(idx)
如果p
没有给出,它将rng.integers
用来获取一个随机值:
rng = numpy.random.default_rng(seed)
idx = rng.integers(0, pop.shape[0]) # yields 9
print(idx)
# -----------------------
rng = numpy.random.default_rng(seed)
idx = rng.choice(pop) # same as above
print(idx)
seed
您可以使用不同的值进行玩耍。我不知道 and 会发生什么rng.random
,rng.integers
但你可以看到它们的行为不同。这就是为什么你得到不同的结果。