在本教程中,我们将讨论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

解释-

让我们理解上述程序中发生了什么:

  1. 由于我们需要使用uniform()方法,我们导入了random模块。
  2. 在此之后,我们初始化了上限和下限,它们分别为8和12。
  3. 最后,我们将这两个值作为参数传递给uniform()。
  4. 执行此程序后,将显示预期的输出。

让我们再看一个程序,其中采用了相同的方法,但是提供了浮点值作为上限和下限。

# 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

解释-

程序的过程与前一个程序相同,但在这里我们可以观察到即使提供了小数值,它也能显示所需的输出。

我们都知道,在Python中使用NumPy模块执行不同的数学操作,由于该模块提供的内置函数的类型,我们的代码变得不那么复杂且更高效。

让我们看看如何在这里使用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 ]

解释-

现在是时候了解上述程序的解释了:

  1. 由于我们需要使用uniform()方法,这次我们导入了NumPy模块。
  2. 接下来是在random.seed()中提供一个值,因为它用于初始化随机数生成器。
  3. 在此之后,我们初始化了数组的大小、上限和下限,它们分别为4、0和1,都在np.random.uniform()中。
  4. 我们使用np.random.uniform()声明了num_arr,因为我们正在生成一个数组。
  5. 执行此程序后,将显示预期的输出,即包含三个值的数组。

现在,让我们看看另一个程序:

# 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]]
<class 'numpy.ndarray'>

解释-

让我们了解这里发生了什么:

  1. 由于我们需要使用uniform()方法,这次我们导入了NumPy模块。
  2. 接下来是在random.seed()中提供一个值,因为它用于初始化随机数生成器。
  3. 在此之后,我们初始化了数组的大小(这次我们创建了一个二维数组)、上限和下限,它们分别为(3,3)、0和1,都在np.random.uniform()中。
  4. 我们使用np.random.uniform()声明了num_arr,因为我们正在生成一个数组。
  5. 执行此程序后,将显示预期的输出,即包含三个值的数组以及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]
<class 'numpy.ndarray'>

解释-

  1. 由于我们需要使用uniform()方法,这次我们导入了NumPy模块。
  2. 接下来是在random.seed()中提供一个值,因为它用于初始化随机数生成器。
  3. 在此之后,我们初始化了数组的大小、上限和下限,它们分别为5、42和63(这次我们采用了明确的范围)都在np.random.uniform()中。
  4. 我们使用np.random.uniform()声明了num_arr,因为我们正在生成一个数组。
  5. 执行此程序后,将显示预期的输出,即包含三个值的数组以及num_arr的类型。

结论

在本教程中,我们学习了什么是uniform()以及它如何在不同的Python程序中使用。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程