## Generate a list of random numbers in python programming language

Hello guys, what’s up? Welcome to our Programiz tutorial in python programming language. Today I am going to teach you about how to generate a list of random numbers in python programming language. Just like other object oriented programming (OOP) language, we can generate random numbers in python programming language. Generating random number in python is a basic program for python programmer. If you don’t know how to generate random numbers in python, you cannot claim yourself as a python programmer. In machine learning algorithm randomness is an important part of the configuration & evaluation. In machine learning we need to

• Split data into random train and test sets
• Random shuffling of a training dataset in stochastic gradient descent
• Random initialization of weights in an artificial neural network
Generate a list of random numbers 0 to 10

What if, if you need to print all random value in between 0 to 10. For this, we can use random.shuffle() function. Because it can shuffle/randomize the number.

Note: range() function is mutually exclusive. So, if we set 10 in range() function, it will never select 10.

```>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]

```

Multiple Random Integers

```>>> import random
>>> lo = 0
>>> hi = 10
>>> size = 5
# A
>>> [lo + int(random.random() * (hi - lo)) for _ in range(size)]
[5, 6, 1, 3, 0]
# B
>>> [random.randint(lo, hi) for _ in range(size)]
[9, 7, 0, 7, 3]
# C
>>> [random.randrange(lo, hi) for _ in range(size)]
[8, 3, 6, 8, 7]
# D
>>> lst = list(range(lo, hi))
>>> random.shuffle(lst)
>>> [lst[i] for i in range(size)]
[6, 8, 2, 5, 1]
# E
>>> [random.choice(range(lo, hi)) for _ in range(size)]
[2, 1, 6, 9, 5]
# F
>>> random.choices(range(lo, hi), k=size)
[3, 2, 0, 8, 2]
# G
>>> random.sample(range(lo, hi), k=size)
[4, 5, 1, 2, 3]```
Generate 10 random numbers in between 1 to 100.
```import random

print(random.sample(range(1, 100), 10))

```

After executing the above code our output will be shown as a list:

```[11, 72, 64, 65, 16, 94, 29, 79, 76, 27]

```
Generate a list of random numbers using numpy module

numpy.random.randint

```import numpy as np
X1 = np.random.randint(low=0, high=10, size=(15,))

print (X1)
>>> array([3, 0, 9, 0, 5, 7, 6, 9, 6, 7, 9, 6, 6, 9, 8])```

numpy.random.uniform

```import numpy as np
X2 = np.random.uniform(low=0, high=10, size=(15,)).astype(int)

print (X2)
>>> array([8, 3, 6, 9, 1, 0, 3, 6, 3, 3, 1, 2, 4, 0, 4])```

random.randrange

```from random import randrange
X3 = [randrange(10) for i in range(15)]

print (X3)
>>> [2, 1, 4, 1, 2, 8, 8, 6, 4, 1, 0, 5, 8, 3, 5]```

random.randint

```from random import randint
X4 = [randint(0, 9) for i in range(0, 15)]

print (X4)
>>> [6, 2, 6, 9, 5, 3, 2, 3, 3, 4, 4, 7, 4, 9, 6]```
Here are some examples of python programming random module

Comparision of Standard Library and Numpy in python programming random module

|——————————————————————————-|

| random                                | numpy.random                                   |

|-|———————– ——-|—————————————– —-|
|A| random()                         | random()                                              |
|B| randint(low, high)          | randint(low, high)                              |
|C| randrange(low, high)    | randint(low, high)                              |
|D| shuffle(seq)                    | shuffle(seq)                                          |
|E| choice(seq)                     | choice(seq)                                          |
|F| choices(seq, k)               | choice(seq, size)                                 |
|G| sample(seq, k)               | choice(seq, size, replace=False)      |
|——————————————————————————-|

Examples

```>>> np.random.normal(loc=5, scale=10, size=size).astype(int)
array([17, 10,  3,  1, 16])

>>> np.random.poisson(lam=1, size=size).astype(int)
array([1, 3, 0, 2, 0])

>>> np.random.lognormal(mean=0.0, sigma=1.0, size=size).astype(int)
array([1, 3, 1, 5, 1])```

Today we have learned many things about python programming language. I hope you guys have understood everything which I have discussed earlier. . So, guys, that’s about how we can generate a list of random numbers in python. Later I will discuss another tutorial on python programming language. Till then, take care. Happy Coding

AllEscort