
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Random Range Average Using Python
Python provides a robust set of tools and libraries to generate random numbers within a specific range and calculate their average. We can use the Numpy library, statistics module, random module, and random.choice function etc to Randomly generate numbers within a range and find their average. In this article, we will use these methods to generate random numbers and find their average.
Algorithm
A general algorithm to generate random numbers and find the average using Python is as follows:
Generate random numbers within a range
Store these numbers in a list or array.
Calculate the average of the generated numbers.
Print the average as output.
Method 1 : Using the Random Module
In Python, random module provides a simple way to generate random numbers. We can use the random.randint(a, b) function to generate random integers within the range [a, b].
Example
In the below example, a function named get_random_range_average is defined that generates a list of n random numbers between a and b using the random.randint() function. It then calculates the average of these numbers and returns it.
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")
Output
Method 1: Using the random module Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 27.3
Method 2 :Using the NumPy library
NumPy is a powerful library for numerical computing in Python. It provides various functions to generate random numbers efficiently. To use NumPy, ensure you have it installed (pip install numpy).
Example
In the example below, the np.random.randint(a, b + 1, size=n) function generates an array of n random integers between a and b. The np.mean() function calculates the average of these numbers.
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")
Output
Method 2: Using the NumPy library Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 48.9
Method 3 :Using the random.choices function
The random.choices() function allows us to generate random numbers with replacement from a given population. We can use this function to generate random numbers within a range.
Example
In the example below, we defined the random.choices() function to generate a list of n random numbers within the range from a to b. It creates a population list using the range() function, then utilizes random.choices() to randomly select numbers from this population. The average of the generated numbers is calculated by summing them and dividing by 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")
Output
Method 3: Using the random.choices function Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 46.9
Method 4 :Using the statistics module
The Python statistics module provides functions to calculate statistical properties. We can use the statistics.mean() function to compute the average of a list of numbers.
Example
In the example below,we use the random.randint() function and the statistics.mean() function to generate a list of n random numbers between a and b. It then calculates the average of these numbers using the statistics.mean() function from the statistics module.
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")
Output
Method 4: Using the statistics module Generated random numbers: [55, 70, 35, 20, 17, 6, 18, 30, 9, 13] Average: 56.9
Conclusion
In this article, we explored the methods to generate random numbers within a range in Python and find their averages. We used the random module, the NumPy library, and the random.choices() function, and the statistics module. Each approach provided the desired output, and you can choose the method that best suits your needs and familiarity with the libraries.