我需要生成一个随机数,实际上我需要从 1 到 70128 的 70128 个随机数。这是我正在使用的:
index = numpy.random.randint(1,70128,70128)
另一件事是,我需要 1 到 70128 之间的每个数字只生成一次。
这意味着我需要一个 1 到 70128 之间的 70128 个随机生成数字的列表,但每个数字只能出现一次。
您需要介于和之间的x
随机数都是唯一的,然后您只需要一个随机范围:1
x
x = 70128
numbers = range(1, x + 1)
random.shuffle(numbers)
如果您使用的是 Python 3,您希望添加list()
对结果的调用range()
。
Python 2.7 上x = 10
的演示具有实用性:
>>> import random
>>> x = 10
>>> numbers = range(1, x + 1)
>>> random.shuffle(numbers)
>>> numbers
[5, 2, 6, 4, 1, 9, 3, 7, 10, 8]
使用 numpy 的random.permutation
函数,如果给定一个标量参数x
,它将返回从 0 到 的数字的随机排列x
。例如:
np.random.permutation(10)
给出:
array([3, 2, 8, 7, 0, 9, 6, 4, 5, 1])
所以,特别是,np.random.permutation(70128) + 1
做你想做的事。