使用 Python 获取随机范围平均值
Python 提供了一套强大的工具和库来生成特定范围内的随机数并计算它们的平均值。我们可以使用 Numpy 库、统计模块、随机模块和 random.choice 函数等来随机生成范围内的数字并找到它们的平均值。在本文中,我们将使用这些方法来生成随机数并找到它们的平均值。
算法
使用 Python 生成随机数并找到平均值的通用算法如下:
生成一定范围内的随机数
将这些数字存储在列表或数组中。
计算生成的数字的平均值。
将平均值打印为输出。
方法 1:使用 Random 模块
在 Python 中,random 模块提供了一种生成随机数的简单方法。我们可以使用 random.randint(a, b) 函数生成 [a, b] 范围内的随机整数。
示例
在下面的示例中,定义了一个名为 get_random_range_average 的函数,该函数使用 random.randint() 函数生成 a 和 b 之间的 n 个随机数列表。然后计算这些数字的平均值并返回它。
import random def get_random_range_average(a, b, n): numbers = [random.randint(a, b) for _ in range(n)] average = sum(numbers) / n return average a = 1 b = 100 n = 10 average = get_random_range_average(a, b, n) print("Method 1: Using the random module") print("Generated random numbers: [55,70,35,20,17,6,18,30,9,13]") print("Average: 27.3")
输出
Method 1: Using the random module Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 27.3
方法 2:使用 NumPy 库
NumPy 是 Python 中用于数值计算的强大库。它提供了各种函数来高效生成随机数。要使用 NumPy,请确保已安装它(pip install numpy)。
示例
在下面的示例中,np.random.randint(a, b + 1, size=n) 函数生成一个包含 a 和 b 之间的 n 个随机整数的数组。np.mean() 函数计算这些数字的平均值。
import numpy as np def get_random_range_average(a, b, n): numbers = np.random.randint(a, b + 1, size=n) average = np.mean(numbers) return average a = 1 b = 100 n = 10 average = get_random_range_average(a, b, n) print("Method 2: Using the NumPy library") print("Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13]") print("Average: 48.9")
输出
Method 2: Using the NumPy library Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 48.9
方法 3:使用 random.choices 函数
random.choices() 函数允许我们从给定的总体中生成有替换的随机数。我们可以使用此函数生成一定范围内的随机数。
示例
在下面的示例中,我们定义了 random.choices() 函数来生成从 a 到 b 范围内的 n 个随机数列表。它使用 range() 函数创建一个总体列表,然后利用 random.choices() 从该总体中随机选择数字。生成的数字的平均值是通过将它们相加并除以 n 来计算的。
import random def get_random_range_average(a, b, n): population = range(a, b + 1) numbers = random.choices(population, k=n) average = sum(numbers) / n return average a = 1 b = 100 n = 10 average = get_random_range_average(a, b, n) print("Method 3: Using the random.choices function") print("Generated random numbers:[55, 70, 35, 20, 17, 6, 18, 30, 9, 13]") print("Average: 46.9")
输出
Method 3: Using the random.choices function Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 46.9
方法 4:使用 statistics 模块
Python statistics 模块提供了计算统计属性的函数。我们可以使用 statistics.mean() 函数来计算数字列表的平均值。
示例
在下面的示例中,我们使用 random.randint() 函数和 statistics.mean() 函数生成 a 和 b 之间的 n 个随机数列表。然后使用 statistics 模块中的 statistics.mean() 函数计算这些数字的平均值。
import random import statistics def get_random_range_average(a, b, n): numbers = [random.randint(a, b) for _ in range(n)] average = statistics.mean(numbers) return average a = 1 b = 100 n = 10 average = get_random_range_average(a, b, n) print("Method 4: Using the statistics module") print("Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13]") print("Average: 56.9")
输出
Method 4: Using the statistics module Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 56.9
结论
在本文中,我们探索了在 Python 中生成一定范围内的随机数并找到其平均值的方法。我们使用了随机模块、NumPy 库、random.choices() 函数和统计模块。每种方法都提供了所需的输出,您可以选择最适合您的需求和对库的熟悉程度的方法。
<