Python Language Creating random integers and floats: randint, randrange, random, and uniform


Example

import random

randint()

Returns a random integer between x and y (inclusive):

random.randint(x, y)

For example getting a random number between 1 and 8:

random.randint(1, 8) # Out: 8

randrange()

random.randrange has the same syntax as range and unlike random.randint, the last value is not inclusive:

random.randrange(100)       # Random integer between 0 and 99
random.randrange(20, 50)    # Random integer between 20 and 49
random.rangrange(10, 20, 3) # Random integer between 10 and 19 with step 3 (10, 13, 16 and 19)

Random distribution graph

random

Returns a random floating point number between 0 and 1:

random.random() # Out: 0.66486093215306317

uniform

Returns a random floating point number between x and y (inclusive):

random.uniform(1, 8) # Out: 3.726062641730108