如何在Python中生成介于0和9(含)之间的随机整数?

例如,0、1、2、3、4、5、6、7、8、9


当前回答

我会尝试以下方法之一:

1.>numpy.random.randint

import numpy as np
X1 = np.random.randint(low=0, high=10, size=(15,))

print (X1)
>>> array([3, 0, 9, 0, 5, 7, 6, 9, 6, 7, 9, 6, 6, 9, 8])

2.>numpy.random.uniform

import numpy as np
X2 = np.random.uniform(low=0, high=10, size=(15,)).astype(int)

print (X2)
>>> array([8, 3, 6, 9, 1, 0, 3, 6, 3, 3, 1, 2, 4, 0, 4])

3.>numpy.random.ochoice

import numpy as np
X3 = np.random.choice(a=10, size=15 )

print (X3)
>>> array([1, 4, 0, 2, 5, 2, 7, 5, 0, 0, 8, 4, 4, 0, 9])

4.>随机范围

from random import randrange
X4 = [randrange(10) for i in range(15)]

print (X4)
>>> [2, 1, 4, 1, 2, 8, 8, 6, 4, 1, 0, 5, 8, 3, 5]

5.>随机随机

from random import randint
X5 = [randint(0, 9) for i in range(0, 15)]

print (X5)
>>> [6, 2, 6, 9, 5, 3, 2, 3, 3, 4, 4, 7, 4, 9, 6]

速度:

► np.random.uniform和np.randm.randint比np.rando.choice、random.randrange和random.randint快得多(大约快10倍)。

%timeit np.random.randint(low=0, high=10, size=(15,))
>> 1.64 µs ± 7.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit np.random.uniform(low=0, high=10, size=(15,)).astype(int)
>> 2.15 µs ± 38.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit np.random.choice(a=10, size=15 )
>> 21 µs ± 629 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit [randrange(10) for i in range(15)]
>> 12.9 µs ± 60.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit [randint(0, 9) for i in range(0, 15)]
>> 20 µs ± 386 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

笔记:

1.>np.random.randint在半开区间(低,高)上生成随机整数。2.>np.random.uniform在半开区间(低,高)上生成均匀分布的数。3.>np.randomchoice在半开区间(低,高)上生成一个随机样本,就好像参数a是np.arange(n)一样。4.>randrange(stop)从range(start,stop,step)生成一个随机数。5.>randint(a,b)返回随机整数N,使得a<=N<=b。6.>astype(int)将numpy数组强制转换为int数据类型。我选择了size=(15,)。这将为您提供长度为15的numpy数组。

其他回答

对于Python 3.6,我有更好的运气

str_Key = ""                                                                                                
str_RandomKey = ""                                                                                          
for int_I in range(128):                                                                                    
      str_Key = random.choice('0123456789')
      str_RandomKey = str_RandomKey + str_Key 

只需添加“ABCD”和“ABCD”或“^!~=-><”等字符要更改要从中提取的字符池,请更改范围以更改生成的字符数。

通过random.shuffle尝试

>>> import random
>>> nums = range(10)
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]

从随机模块的文档页面:

警告:此模块的伪随机生成器不应用于安全目的。如果需要,请使用os.urantom()或SystemRandom需要密码安全的伪随机数发生器。

Python 2.4中引入的random.SystemRandom被认为是加密安全的。它在Python 3.7.1中仍然可用,在编写时是最新的。

>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'

除了字符串数字,还可以对其他一些答案使用范围,也许还可以理解。根据您的需要进行混合和搭配。

from random import randint

x = [randint(0, 9) for p in range(0, 10)]

这将生成范围为0到9(含0到9)的10个伪随机整数。

您可以尝试从Python导入随机模块,然后让它在九个数字之间进行选择。这真的很基本。

import random
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    

如果以后要使用,可以尝试将计算机选择的值放在变量中,但如果不使用,打印功能应该这样工作:

choice = random.choice(numbers)
print(choice)