📜  在Python中生成随机数列表

📅  最后修改于: 2022-05-13 01:54:49.706000             🧑  作者: Mango

在Python中生成随机数列表

有时,在制作游戏或赌博程序时,我们会遇到使用随机数创建列表的任务。该任务通常是使用循环并一一附加随机数来执行。但是总是需要以最简洁的方式执行此操作。让我们讨论一些可以做到这一点的方法。

方法 #1:使用列表理解 + randrange()

使用列表推导可以缩短执行此特定任务的简单方法。 randrange函数用于执行生成随机数的任务。

Python3
# Python3 code to demonstrate
# to generate random number list
# using list comprehension + randrange()
import random
 
# using list comprehension + randrange()
# to generate random number list
res = [random.randrange(1, 50, 1) for i in range(7)]
 
# printing result
print ("Random number list is : " +  str(res))


Python3
# Python3 code to demonstrate
# to generate random number list
# using random.sample()
import random
 
# using random.sample()
# to generate random number list
res = random.sample(range(1, 50), 7)
 
# printing result
print ("Random number list is : " +  str(res))


Python3
# importing numpy module
import numpy as np
 
# print the list of 10 integers from 3  to 7
print(list(np.random.randint(low = 3,high=8,size=10)))
 
# print the list of 5 integers from 0 to 2
# if high parameter is not passed during
# function call then results are from [0, low)
print(list(np.random.randint(low = 3,size=5)))


Python3
import numpy as np
 
# generates list of 4 float values
print(np.random.random_sample(size = 4))
 
# generates 2d list of 4*4
print(np.random.random_sample(size = (4,4)))


Python3
import random
 
rand_list=[]
n=10
for i in range(n):
    rand_list.append(random.randint(3,9))
print(rand_list)


输出
Random number list is : [30, 48, 14, 33, 1, 4, 18]


方法 #2:使用 random.sample()

这个单一的效用函数执行问题陈述所要求的确切要求,它产生了 N 号。指定范围内的列表中的随机数并返回所需的列表。

Python3

# Python3 code to demonstrate
# to generate random number list
# using random.sample()
import random
 
# using random.sample()
# to generate random number list
res = random.sample(range(1, 50), 7)
 
# printing result
print ("Random number list is : " +  str(res))
输出
Random number list is : [21, 1, 14, 22, 6, 39, 7]

方法 #3:使用 numpy.random

numpy 模块提供的 random函数对您来说可能更有用,因为与 random 模块相比,它提供了更好的功能和性能。

1. 使用 numpy.random.randint函数生成随机整数列表

此函数从整数数据类型的“离散均匀”分布中返回随机整数。

Python3

# importing numpy module
import numpy as np
 
# print the list of 10 integers from 3  to 7
print(list(np.random.randint(low = 3,high=8,size=10)))
 
# print the list of 5 integers from 0 to 2
# if high parameter is not passed during
# function call then results are from [0, low)
print(list(np.random.randint(low = 3,size=5)))
Output: 
[5, 3, 6, 7, 4, 5, 7, 7, 7, 7]
[0, 2, 1, 2, 1]

2. 使用 numpy.random.random_sample函数生成随机浮点值列表

此函数以半开区间 [0.0, 1.0) 返回随机浮点值。

Python3

import numpy as np
 
# generates list of 4 float values
print(np.random.random_sample(size = 4))
 
# generates 2d list of 4*4
print(np.random.random_sample(size = (4,4)))
output:
[0.08035145 0.94966245 0.92860366 0.22102797]
[[0.02937499 0.50073572 0.58278742 0.02577903]
 [0.37892104 0.60267882 0.33774815 0.28425059]
 [0.57086088 0.07445422 0.86236614 0.33505317]
 [0.83514508 0.82818536 0.1917555  0.76293027]]

在Python的 random 模块上使用 numpy.random 的好处是它提供了一些额外的概率分布,这有助于科学研究。

方法四:使用随机模块

通过使用 random.randint() 我们可以将随机数添加到列表中。

Python3

import random
 
rand_list=[]
n=10
for i in range(n):
    rand_list.append(random.randint(3,9))
print(rand_list)
输出
[8, 8, 8, 9, 5, 6, 5, 3, 7, 9]