random.random range python code example

Example 1: python how to generate random number in a range

import random

# generates completely random number
x = random.random()

# generates a random int
x = random.randint()

# generates a random int in a range
x = random.randint(1, 100)

# NOTE: RANGE DOESN'T WORK FOR random.random()

Example 2: python random

# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))
# random generates a random real number in the interval [0, 1)
print(random.random())

Example 3: random.uniform python

#In contrast to randInt .random.uniform generates a floating number between two variables
# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))

Example 4: random in range

// Get a random number between 20 and 30 
Math.round((Math.random() * (30 - 20 + 1)) + 20);