How to Generate a Random Number Between 2 Numbers in Python


If you need to generate a random number between two (2) numbers in Python, then you can make use of the random module.

First, import the module:

import random

Then you can use it like this:

random.randint(0, 255)

Another option is to use randrange and uniform as follows:

from random import randrange, uniform

# randrange gives you an integral value
irand = randrange(0, 10)

# uniform gives you a floating-point value
frand = uniform(0, 10)