Use the random.random() function

#import the module
import random

#Get a random value between 0.0 and 1.0
random_value = random.random()

print(random_value)

The random() function in the random module simply gets a random floating point value between 0.0 and 1.0. 

random()

The function does not take any argument.

import random

print(random.random())

Scaling up the returned value

Whenever we want a larger number, we can scale up the value returned by the random() function by multiplying it with a larger number, like 10. For example if we want a random number between 0 and 10 we can simply multiply the value with 10 then cast it to an integer.

use the random() function to get a larger integer

#import the module
import random

random_value = random.random()
print('before: ', random_value)

#Scale the value up
print('after: ', int(random_value * 10))

Get a random value between 0 and 100

#import the module
import random

random_value = random.random()
print('before: ', random_value)

#Scale the value up
print('after: ' , int(random_value * 100))

get a random value between 0 and 1000

#import the module
import random

random_value = random.random()
print('before: ', random_value)

#Scale the value up
print('after: ', int(random_value * 1000))

Get a list of random values

We can use this method to create a list containing random values.

Get a list of random values

#import the module
import random

random_values = [0] * 10

for i in range(10):
   random_values[i] = random.random()

print(random_values)

Get a list of random integers between 0 and 100

#import the module
import random

random_values = [0] * 10

for i in range(10):
   random_values[i] = random.random()

random_ints = [ i for i in  map(lambda x: int(x * 100), random_values) ]

print(random_ints)

In the above example, the for loop iterates through the list and assigns each value to a random number generated by the random.random() function. We then use  the map and lambda functions to multiply each random number by 100 and convert it to an integer. The result is a list of 10 random integers ranging from 0 to 100.