# How to generate a random number between 0 and 1 in python ?

Published: April 14, 2020

Examples of how to generate a random number between 0 and 1 in python

### Using function random.uniform()

To generate a random number between 0 and 1, there are several solutions for example using the random module with uniform():

````>>> import random`
`>>> x = random.uniform(0,1)`
`>>> x`
`0.24773029475050623`
```

Generate a list of random numbers between 0 and 1:

````>>> list_rx = [random.uniform(0,1) for i in range(10000)]`
```

and plot:

````>>> import matplotlib.pyplot as plt`
`>>> plt.hist(list_rx,density=1)`
`(array([0.96310412, 0.95410315, 1.04511299, 1.05211375, 1.01811007,`
`       1.04211267, 0.98210618, 1.00010812, 0.97710564, 0.96710456]), array([7.53999199e-05, 1.00064589e-01, 2.00053777e-01, 3.00042966e-01,`
`   4.00032155e-01, 5.00021344e-01, 6.00010532e-01, 6.99999721e-01,`
`   7.99988910e-01, 8.99978098e-01, 9.99967287e-01]), <a list of 10 Patch objects>)`
`>>> plt.show()`
```

### Using numpy random.uniform

Another solution is to generate a matrix with random numbers between 0 and 1 using numpy:

````>>> import numpy as np`
`>>> R = np.random.uniform(0,1,10)`
`>>> R.shape`
`(10,)`
`>>> R`
`array([0.78628896, 0.16248914, 0.01916588, 0.37004623, 0.94038203,`
`   0.68926777, 0.13643452, 0.62616651, 0.69852288, 0.27889004])`
```

Image

of