作者:傻瑶叫一下来人fd_478 | 来源:互联网 | 2023-10-10 16:51
在本教程中,我们将讨论 Python random
模块中的 uniform()方法,以及如何通过导入 NumPy 来使用它。
uniform()返回一个浮点数或指定限制之间的数字范围。
使用 uniform()的语法是-
random.uniform(a,b)
这里,“a”代表上限,b 代表下限。
让我们看一些例子,展示它在 Python 程序中的用法。
下面的程序说明了如何在 python 程序中使用它
# Import the random module
import random
# Initialize the upper and lower limits
x = 8
y = 12
# Displaying the random number
print ("The random number between 8 and 12 is : ", end = " ")
#using random.uniform()
print(random.uniform(x,y))
输出:
The random number between 8 and 12 is: 10.14646142251812
解释-
让我们了解一下在上面的程序中发生了什么-
- 因为我们必须使用 uniform()方法,所以我们导入了
random
模块。
- 之后,我们初始化了上限和下限,分别是 8 和 12。
- 最后,我们将这两个值作为统一()中的参数传递。
- 执行该程序时,会显示预期的输出。
让我们看一看另一个程序,其中我们遵循了相同的方法,但提供了浮动值作为上限和下限。
# Import the random module
import random
# Initialize the upper and lower limits
x = 9.7
y = 14.3
# Displaying the random number
print("The random number between 9.7 and 14.3 is: ", end = " ")
#using random.uniform()
print(random.uniform(x,y))
输出:
The random number between 9.7 and 14.3 is : 11.521121715281813
解释-
该程序与之前的程序相同,但在这里我们可以观察到,即使我们提供十进制值,它也显示所需的输出。
我们都知道,NumPy 模块在 Python 中用于执行不同的数学运算,由于该模块提供的内置函数种类,我们的代码变得不那么复杂,效率更高。
让我们看看如何在这里使用 uniform()
考虑下面给出的程序,
# Importing the NumPy module
import numpy as np
np.random.seed(55)
# Creating an array of size four
num_arr = np.random.uniform(size = 4, low = 0, high = 1)
# Displaying the values of array
print("The resultant array is: ", num_arr)
输出:
The resultant array is: [0.09310829 0.97165592 0.48385998 0.2425227 ]
解释-
是时候知道上面程序的解释了-
- 因为我们必须使用 uniform()方法,所以这次我们导入了 NumPy 模块。
- 下一步是在 random.seed()中提供一个值,因为它用于初始化随机数生成器。
- 之后,我们在 np.random.uniform()中初始化了数组大小的值,分别是 4,0 和 1 的上界和下界。
- 我们已经使用 np.random.uniform()声明了 num_arr,因为我们正在这里生成一个数组。
- 在执行这个程序时,会显示预期的输出,它是一个由三个值组成的数组。
现在,让我们看看另一个程序-
# Importing the numpy module
import numpy as np
np.random.seed(0)
# Creating an array of size four
num_arr = np.random.uniform(size = (3, 3), low = 0, high = 1)
#Displaying the values of array
print("The resultant array is: ", num_arr)
# Displaying the type of num_arr
print(type(num_arr))
输出:
The resultant array is: [[0.5488135 0.71518937 0.60276338]
[0.54488318 0.4236548 0.64589411]
[0.43758721 0.891773 0.96366276]]
解释-
让我们了解这里发生了什么,
- 因为我们必须使用 uniform()方法,所以这次我们导入了 NumPy 模块。
- 下一步是在 random.seed()中提供一个值,因为它用于初始化随机数生成器。
- 之后,我们初始化了数组的大小值(这次我们创建了一个二维数组),上限和下限分别是(3,3),0 和 1,在 np.random.uniform()中。
- 我们已经使用 np.random.uniform()声明了 num_arr,因为我们正在这里生成一个数组。
- 在执行这个程序时,会显示预期的输出,它是一个由三个值组成的数组,并且是 num_arr 类型。
最后,是时候讨论本文的最后一个节目了,
#Importing the numpy module
import numpy as np
np.random.seed(0)
#Creating an array of size five
num_arr=np.random.uniform(size = 5, low = 42, high = 63)
#Displaying the values of array
print("The resultant array is: ", num_arr)
#Displaying the type of num_arr
print(type(num_arr))
输出:
The resultant array is: [53.52508358 57.01897669 54.6580309 53.44254684 50.89675079]
解释-
- 因为我们必须使用 uniform()方法,所以这次我们导入了 NumPy 模块。
- 下一步是在 random.seed()中提供一个值,因为它用于初始化随机数生成器。
- 之后,我们初始化了 np.random.uniform()中数组大小的值,上界和下界分别为 5,42 和 63(这次我们取了一个确定的范围)。
- 我们已经使用 np.random.uniform()声明了 num_arr,因为我们正在这里生成一个数组。
- 在执行这个程序时,会显示预期的输出,它是一个由三个值组成的数组,并且是 num_arr 类型。
结论
在本教程中,我们学习了什么是 uniform()以及如何在各种 python 程序中使用它。