Python 如何生成随机数

Python 如何生成随机数

Python包含一个内置的random模块,用于生成随机数。

在本文中,我们将展示如何使用不同的方法在Python中生成随机数:

  • 使用random.seed()方法

  • 使用random.randrange()方法

  • 使用random.randint()方法

  • 使用random.random()方法

  • 使用random.choice()方法

  • 使用random.uniform()方法

方法1:使用random.seed()方法

随机数生成器通过seed()方法进行初始化。

要生成一个随机数,随机数生成器需要一个起始的数值(种子值)。

注意: 默认情况下,随机数生成器使用当前系统时间。

如果要改变随机数生成器的起始数值,请使用seed()方法。

如果您两次使用相同的种子值,将获得相同的随机数。

语法

random.seed(x, version)

参数

x(可选) − 生成随机数所需的种子值。如果它是一个整数,将直接使用它;否则,必须将其转换为整数。

version − 一个整数,指定如何将’x’参数转换为整数。默认值为2。如果该值为None,生成器将使用当前系统时间。

以下程序使用random.random()和seed()方法从列表中返回一个随机元素−

import random
# Setting the seed value to 5
random.seed(5)
print(random.random())

# Setting the same seed value as above i.e 5
random.seed(5)
print(random.random())

输出

执行上述程序后,将生成以下输出 –

0.62290169489
0.62290169489

我们将种子值设置为5,然后使用random模块的random()函数生成一个随机值。然后我们再次将种子值设置为5,并使用random()函数生成与之前相同的随机值。这就是seed()函数的用法。

方法2:使用random.randrange()方法

randrange()方法从指定范围中随机选择一个元素并返回它。

语法

random.randrange(start, stop, step)

参数

start(optional)(可选) − 一个整数,表示开始位置。默认为0。

stop(required)(必选) − 一个整数,表示结束位置。

步骤(optional)(可选) − 一个整数,表示增量。默认为1。

以下程序使用randrange()函数返回给定范围内的随机数 −

import random

# generating a random number between 10(included) and 20(not included)
print("Random Number Generated = ", random.randrange(10, 20))

输出

在执行后,上述程序将产生以下输出:

Random Number Generated = 13

randrange()函数在这里用于从一个范围内生成一个随机数。我们将起始值(下限)和结束值(上限)作为参数传递,它将生成一个在这些范围内的随机数

方法3: 使用random.randint()方法

randint()方法返回一个整数值,代表从给定范围中随机选择的元素。

注意 −randint()方法是randrange(start, stop+1)的别名。

语法

random.randint(start, stop)

参数

start(必需) - 表示起始位置的整数。

stop(必需) - 表示结束位置的整数。

以下程序使用randint()函数返回给定范围内的随机数 –

import random
# generating a random number between 10 and 20(both 10 and 20 numbers included)
print("Random Number Generated = ", random.randint(10, 20))

输出

执行时,以上程序将生成以下输出 –

Random Number Generated = 20

randrange()和randint之间的区别在于randint包括范围内的上限,而randrange()不包括上限。我们可以向randrange()函数添加步长值,但不能向randint()函数添加步长值。

方法4:使用random.random()

random()方法生成一个在0和1之间的随机浮点数值。

语法

random.random()

参数

这个random()方法不接受任何参数

以下程序使用random()函数返回给定范围内的随机数

import random
# generating a random floating-point number between 0 and 1
print("Random Number Generated = ", random.random())

输出

Random Number Generated = 0.15685132230226662

方法5:使用random.choice()方法

choices()方法返回一个包含从提供的序列中随机选择的元素的列表。

序列可以是字符串、范围、列表、元组或其他任何类型。

以下程序从列表中返回随机数−

import random

#Input list
given_List = [ 1, 6, 3, 9, 10, 24, 475, 483, 2656]
print('The first Random Element from the list:',random.choice(given_List))
print('The Second Random Element from the list:',random.choice(given_List))

输出

The first Random Element from the list: 6
The Second Random Element from the list: 24

方法6:使用random.uniform()方法

uniform()方法在两个输入值之间生成一个随机浮点数(包括两个数字)。

语法

random.uniform(x, y)

参数

x(必填) − 表示最小可能结果的数字

y(必填) − 表示最大可能结果的数字

以下程序使用uniform()函数返回给定范围内的随机浮点数 –

import random
# generating a random number between 10 and 20(both 10 and 20 are also included)
print("Random Number Generated = ", random.uniform(10, 20))

输出

Random Number Generated = 12.845596876772472

在这个示例中,uniform()函数被用来生成一个在范围内的随机浮点数。我们将起始值(下限)和结束值(上限)作为参数传递给它,它会生成一个在这两个范围之间的随机浮点数/小数。

结论

在本文中,我们学习了如何使用六种不同的方法生成随机数:

seed()、randrange()、randint()、choice()、random()、uniform()。我们还学习了如何使用choice()函数从列表/字符串/元组中获取一个随机元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程