Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Random Numbers Summation

Python – Random Numbers Summation

Sometimes, in making programs for gaming or gambling, we come across the task of creating the list all with random numbers and perform its summation. This task has to performed in general using loop and appending the random numbers one by one and then performing sum. But there is always requirement to perform this in most concise manner. Lets discuss certain ways in which this can be done. 

Method #1 : Using list comprehension + randrange() + sum() The naive method to perform this particular task can be shortened using the list comprehension. randrange function is used to perform the task of generating the random numbers. The task of performing sum is done using sum(). 

Python3




# Python3 code to demonstrate
# Random Numbers Summation
# using list comprehension + randrange() + sum()
import random
 
# using list comprehension + randrange() + sum()
# Random Numbers Summation
res = sum([random.randrange(1, 50, 1) for i in range(7)])
 
# printing result
print ("Random number summation list is : " + str(res))


Output : 

Random number summation list is : 187

Time complexity: O(n), where n is the length of the test_list. The list comprehension + randrange() + sum() takes O(n) time
Auxiliary Space: O(n), extra space of size n is required

  Method #2 : Using random.sample() + sum() This single utility function performs the exact required as asked by the problem statement, it generated N no. of random numbers in a list in the specified range and returns the required list. The task of performing sum is done using sum(). 

Python3




# Python3 code to demonstrate
# Random Numbers Summation
# using random.sample() + sum()
import random
 
# using random.sample() + sum()
# Random Numbers Summation
res = sum(random.sample(range(1, 50), 7))
 
# printing result
print ("Random number summation list is : " + str(res))


Output : 

Random number summation list is : 187

Time Complexity: O(n), where n is the number of elements
Auxiliary Space: O(1), constant extra space is required

 Method #3 : Using random.randint()

In this method, n is the number of random integers you want to generate, a and b define the range of the random integers (inclusive of a and b), and rand_nums is a list that contains the random integers. The random.randint(a, b) function generates a random integer between a and b, inclusive of both a and b. We use a list comprehension to generate a list of n random integers between a and b. The sum function is then used to calculate the sum of the random integers in the list rand_nums. Finally, we print out the list of random integers and the sum of those integers. You can adjust the values of n, a, and b to generate different numbers of random integers and change the range of the random integers.

Python3




import random
 
# generate n random integers between a and b
n = 10
a = 1
b = 10
rand_nums = [random.randint(a, b) for _ in range(n)]
 
# calculate the sum of the random integers
rand_sum = sum(rand_nums)
 
# print the results
print("Random integers:", rand_nums)
print("Sum of random integers:", rand_sum)


Output

Random integers: [6, 8, 5, 7, 6, 7, 2, 4, 1, 3]
Sum of random integers: 49

Time complexity: O(n)

Auxiliary Space: O(n)

Method #4 :  Using reduce() from the functools module:

1.Generate n random integers between a and b using the random module and list comprehension. Store the list of random integers in the variable rand_nums.

2.Initialize a variable rand_sum to 0.

3.Loop through each integer in rand_nums. For each integer, add it to the current value of rand_sum.

4.After the loop is complete, rand_sum will hold the sum of all the random integers.

5.Print the list of random integers and the sum of the random integers.

Python3




import random
from functools import reduce
 
# generate n random integers between a and b
n = 10
a = 1
b = 10
rand_nums = [random.randint(a, b) for _ in range(n)]
 
# calculate the sum of the random integers using reduce()
rand_sum = reduce(lambda x, y: x + y, rand_nums)
 
# print the results
print("Random integers:", rand_nums)
print("Sum of random integers:", rand_sum)
#This code is contributed by Jyothi pinjala


Output

Random integers: [7, 4, 3, 4, 9, 9, 9, 2, 6, 2]
Sum of random integers: 55

The time complexity:O(n), where n is the number of random integers generated. This is because generating a list of n random integers takes O(n) time, and calculating the sum of the integers using the reduce() function also takes O(n) time.

The auxiliary space :O(n), as the list of n random integers and the output variable rand_sum both take up O(n) space.

RELATED ARTICLES

Most Popular

Recent Comments