لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy Random Introduction

Defination: A random means something that can not be predicted logically, it does NOT mean a different number every time.

Pseudo Random and True Random

As computer programs are definitive set of instructions, this means there must be some algorithm to generate a random number as well.

This makes it clear that If there is a program to generate random number it can be predicted, thus it is not truly random.

The numbers which are generated through a generation algorithm are called pseudo random.

The question in the light of above is whether Can we make truly random numbers?

The answer to the above question is Yes! In order to generate a truly random number on our computers we need to get the random data from some outside source.

This outside source is generally comprised of our keystrokes, mouse movements, data on network etc.

Thus we do not need truly random numbers, unless its related to security (e.g. encryption keys) or the basis of application is the randomness (e.g. Digital roulette wheels).

So in this tutorial we will be using pseudo random numbers for our learning.

How to generate random numbers?

Example 1: Generate a random integer between 0 and 50.

Code

from numpy import random

x = random.randint(50)

print(x)

the output will be

32

Note: Each time the code is run the output will be different as number is generated randomly.

This demonstrate the defination we have given initially that random means something which can not be predicted logically, it does NOT mean a different number every time.

Though you get a random number generated every time, it can not be logically predicted.

How to generate random Float?

The rand() method in random module returns a random float between 0 and 1.

Example 2: Generate a random integer between 0 and 1.

Code

from numpy import random

x = random.rand()

print(x)

the output will be

0.3061685009679529

Note: Each time the code is run the output will be different as number is generated randomly.

Generating Random Array

You can use the two methodss viz randint() and rand() from the above examples to make random arrays in NumPy.

The randint() method takes a size parameter where you can specify the shape of an array.

The rand() method takes a size parameter also where you can specify the shape of an array but values will be returned between 0 and 1.

Example 3: Generate a 1-D array containing 3 random integers from 0 to 50.

Code

from numpy import random

x=random.randint(50, size=(3))

print(x)

the output will be

[19 13 9]

Note: Each time the code is run the output will be different as number is generated randomly.

Example 4: Generate a 2-D array with 3 rows, each row containing 3 random integers from 0 to 50.

Code

from numpy import random

x = random.randint(50, size=(3, 3))

print(x)

the output will be

```[[40 41 29]
[ 9 43 31]
[30 12 12]]
```

Note: Each time the code is run the output will be different as number is generated randomly.

Float

The rand() method also allows you to specify the shape of the array.

Example 6: Generate a 2-D array with 3 rows, each row containing 3 random numbers.

Code

from numpy import random

x = random.rand(3, 3)

print(x)

the output will be

[[0.74859758 0.7075546 0.5117638 ]

[0.41808371 0.3088195 0.3741289 ]

[0.82776838 0.9520432 0.73855662]]

Note: Each time the code is run the output will be different as number is generated randomly but because you are generating float the values will be returned between 0 & 1.

Example 5: Generate a 1-D array containing 4 random floats.

Code

from numpy import random

x = random.rand(4)

print(x)

the output will be

[0.52544452 0.68910017 0.74165553 0.7148002 ]

Note: Each time the code is run the output will be different as number is generated randomly but because you are generating float the values will be returned between 0 & 1.

Generating Random Number From Array

In NumPy we use the choice() method which allows you to generate a random value based on an array of values.

How it works?

The choice() method takes an array as a parameter and randomly returns one of the values from the array.

Example 6: Return one of the values in an array.

Code

from numpy import random

x = random.choice([4, 5, 6, 7, 9])

print(x)

the output will be

4

Note: Each time the code is run the output will be different as number is generated randomly, It will randomly return a value from the given array.

The choice() method also allows you to return an array of values.

You can add a size parameter also to specify the shape of the array.

Example 7: Generate a 2-D array that consists of the values in the array parameter (4, 6, 7, and 8).

Code

from numpy import random

x = random.choice([4, 6, 7, 8], size=(3, 5))

print(x)

the output will be

```[[4 8 6 7 7]
[7 6 7 4 4]
[7 8 6 4 8]]
```

Note: Each time the code is run the output will be different as number is generated randomly.

Since we have specified to generate a 2-D array that consists of the values in the array parameter (4, 6, 7, and 8).

Also we have specified the size as 3 rows and 5 integers.

But as you can see that the given array has only 4 integers.

So the 5th one has been generated randomly from the given array and 3 row have been returned with 5 integers.