How to generate random numbers in Python?

Random numbers are used for many purposes, let’s take some examples to better understand how are work.

First example about how to generate random numbers in Python

Write a program that generates n random numbers in an interval set by the user and displays them in output.

So, first of all, let’s import the random module.

Then we ask you to enter the interval values ​​that we store in two variables, for example named x and y.

We also ask you to enter the amount of random numbers to be generated.


import random

x = int(input('Insert the first number : '))
y = int(input('Insert the first number : '))

n = int(input('How many numbers? : '))

Now we can generate the random numbers with the for loop, using randint in the interval (x, y).


import random

x = int(input('Insert the first number : '))
y = int(input('Insert the first number : '))

n = int(input('How many numbers? : '))

for i in range(n):
  random_number = random.randint(x,y)
  print(random_number,end=' ')

But be careful if the user enters the value of y smaller than x?

The program returns an error at the randint point (x, y) because the value of x must be less than the value of y.

Therefore, we can check the data entered by requesting the user to re-enter the data. Or you can also swap the two variables.

By exchanging the variables we therefore have:


import random

x = int(input('Insert the first number : '))
y = int(input('Insert the first number : '))

if x > y:
    x,y = y,x

n = int(input('How many numbers? : '))

for i in range(n):
  random_number = random.randint(x,y)
  print(random_number,end=' ')

With an input check we could instead write like this:


import random

x = int(input('Insert the first number : '))
y = int(input('Insert the second number : '))

while x > y:
    print('The first number must be smaller than second!')
    x = int(input('Insert the first number : '))
    y = int(input('Insert the second number : '))

n = int(input('How many numbers? : '))

for i in range(n):
  random_number = random.randint(x,y)
  print(random_number,end=' ')

Second example about how to generate random numbers in Python

Generate 100 random numbers from 1 to 200 in steps of 2, sum them and make the average.

In steps of 2 from 1 to 200-1, it means that only odd numbers from 1 to 199 are generated.

So let’s use the randrange function for this purpose which, unlike randint, also allows us to indicate the step.

Here, then, is the complete code of the algorithm on random numbers in Python:


import random

n = 100
sum = 0

for i in range(n):
  random_number = random.randrange(1,200,2)
  print(random_number,end=' ')
  sum += random_number

average= sum / n

print('Average: ', average)

Clearly the calculation of the average is carried out as we have done many times with non-random numbers.

Conclusion

In this lesson we have developed some simple exercises on how to generate random numbers in Python, in the next lessons we will continue to practice again.

Useful links

Python tutorial

Introduzione al linguaggio Python

Create Python matrix

Python lambda function

Python test Lambda Function

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *