 # Random numbers

Using the random module, we can generate pseudo-random numbers. The function random() generates a random number between zero and one [0, 0.1 .. 1].  Numbers generated with this module are not truly random but they are enough random for most purposes.

Related Course:
Python Programming Bootcamp: Go from zero to hero

Random number between 0 and 1.
We can generate a (pseudo) random floating point number with this small code:

Generate a random number between 1 and 100
To generate a whole number (integer) between one and one hundred use:

This will printa random integer. If you want to store it in a variable you can use:

Random number between 1 and 10
To generate a random floating point number between 1 and 10 you can use the uniform() function

Picking a random item from a list

Fun with lists
We can shuffle a list with this code:

To pick a random number from a list:

We can do the same thing with a list of strings:

## 20 thoughts on “Random numbers”

1. - January 31, 2018

Hello Frank,
I can’t run everything in this tutorial. why?

`Traceback (most recent call last): File "random.py", line 13, in from random import * File "/home/insight/Desktop/python/pyspot/random.py", line 16, in shuffle(items)NameError: name 'shuffle' is not defined`

1. - February 8, 2018

You named your program “random.py”, give it a different name because Python is trying to import from the module random.

2. - June 3, 2017

Quick thanks for the great comments thread – it elevated a my super basic reference search to a whole ‘nother level – kudos!