Let’s say you need to generate random numbers in Python. I need it once for an application that generates random numbers in order to create math problems to be solved by students. But being able to generate random numbers is useful in many other situations. In order to generate random numbers in Python we need to import the
If you just need a random number between 0.0 and 1.0 then just call
>>> import random
You might want to generate numbers within a certain range. For that you have to import
randint which stands for “random integer”.
>>> from random import randint
>>> n1 = randint(1, 99)
>>> n2 = randint(1, 99)
>>> n3 = n1 + n2
n2 generate random integer between 1 and 99.
You can also make a random choice among a number of possibilities by using the
>>> numbers = [16, 6, 1977, 84, 5, 7, 2007]
If you need then you can be very creative with the