The random()
function in the random module
simply gets a random floating point value between 0.0 and 1.0.
random()
copy
The function does not take any argument.
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.
Get a list of random values
We can use this method to create a list containing random values.
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.