使用 Python 獲取隨機範圍平均值
Python 提供了一套強大的工具和庫,用於在特定範圍內生成隨機數並計算其平均值。我們可以使用 Numpy 庫、statistics 模組、random 模組和 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)** 函式生成一個包含 n 個 a 和 b 之間隨機整數的陣列。**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 中生成指定範圍內隨機數並找到其平均值的方法。我們使用了 random 模組、NumPy 庫和 random.choices() 函式以及 statistics 模組。每種方法都提供了所需的輸出,您可以選擇最適合您的需求和對庫的熟悉程度的方法。