# Finding the Summation of Random Numbers using Python

In this article we will learn about different methods to find the Summation of Random Numbers using Python language. In some scenarios we need to generate some random number and find its summation so we will see basic to improved versions of functions using which we can get this task done.

Let’s see some methods to find the summation of random numbers.

## Method 1. Using Simple Loop

### Example

import random
n = 10
rand_nums = []
for _ in range(n):
rand_nums.append(random.randint(1, 100))
total = sum(rand_nums)

print("Random Numbers:", rand_nums)
print("Summation of random numbers:", total)


### Output

Random Numbers: [84, 67, 73, 29, 55, 69, 54, 76, 53, 85]
Summation: 645


### Explanation

In this example we are generating 10 random numbers by creating a list comprehension and using the sum function we are calculating the summation of the random generated numbers.

## Method 2. Using NumPy.

### Example

import numpy as np

n = 10
rand_nums = np.random.randint(1, 101, n)
total = np.sum(rand_nums)

print("Random Numbers:", rand_nums)
print("Summation of random numbers:", total)


### Output

Random Numbers: [53 25 52 28 37 19 18 89 57 35]
Summation of random numbers: 413


### Explanation

Here in the above program we imported numpy and we generated 10 random numbers which range between 1 to 100 using the np.random.randint() function. After generating random number we calculate the summation of these generated number using np.sum().

## Method 3. Using While Loop

### Example

import random

requied_sum = 100
summ = 0
rand_nums = []

while summ < requied_sum:
num = random.randint(1, 20)
if summ + num <= requied_sum:
rand_nums.append(num)
summ += num

print("Random Numbers:", rand_nums)
print("Summation of random numbers:", summ)


### Output

Random Numbers: [14, 19, 4, 19, 17, 3, 5, 8, 9, 2]
Summation of random numbers: 100


### Explanation

Here in the above program we imported random module to generate random numbers. We take a variable required_sum which is our target sum until the sum we get equal to or greater than target we will continue generating random numbers and taking sum into our summ variable. Here rand_nums is a list which is used to append the generated numbers.

## Method 4. Using Random.simple() Function

### Example

import random

n = 10
rand_nums = random.sample(range(1, 101), n)
total = sum(rand_nums)

print("Random Numbers:", rand_nums)
print("Summation of random numbers:", total)


### Output

Random Numbers: [90, 87, 76, 22, 77, 6, 82, 63, 53, 3]
Summation of random numbers: 559


### Explanation

Here in the above program we imported random module which is used to generate the random numbers. We generated 10 random numbers which range between 1 to 100 using the random.simple() function. random.simple() function is used to generate unique set of random numbers. After we generated the random number we calculated the sum into total variable using the sum method.

## Method 5. Using Generator Expression

### Example

import random

n = 10
rand_nums = [random.randint(1, 100) for _ in range(n)]
total = sum(rand_nums)

print("Random Numbers:", list(rand_nums))
print("Summation of random numbers: ", total)


### Output

Random Numbers: [34, 89, 92, 36, 3, 54, 79, 16, 22, 12]
Summation of random numbers: 437


### Explanation

Here in the above program, we imported random module for generating random numbers. We take variable as total number n of random numbers which is to be generated. We will use the generator expression to generate the random number and using sum function we will add that generated number into the rand_sum variable. Then we will convert the generator expression into the list.

So, we get to know about how we can generate random numbers and find the summation of that generated numbers. We saw using various methods like simple loop, generator function, numpy to find the summation of random generated numbers.

Updated on: 03-Oct-2023

173 Views