# NumPy Random Numbers

In this comprehensive guide, we will explore NumPy random number generation functions, including how to generate different types of random numbers, how to set random seeds for reproducibility, and how to simulate random data distributions.

NumPy has a powerful set of random number generation functions that can be used to create random data for various scientific and statistical applications.

## What is NumPy Random Number?

NumPy random number is a set of functions provided by the NumPy library to generate random numbers for various scientific and statistical applications.

These functions can generate different types of random numbers, such as uniform, normal, integer, and more.

NumPy random number generation functions are efficient and provide a high degree of control over the random number generation process.

The NumPy random number generation functions are widely used in data analysis, simulation, and other scientific applications that require the generation of random data.

## Pseudo Random and True Random.

The random number generation process can be set to generate pseudo-random numbers, which can be replicated by setting the random seed, ensuring reproducibility of the results.

Computers interact with programs, and programs are collections of commands.

To generate a random number, there must also be an algorithm.

Random numbers can be predicted if a program produces them, so they are not actually random.

Pseudo random numbers are produced utilizing a generation algorithm in Numpy random.

## Generating NumPy random numbers

It is necessary to get random data for our computers from an external source in order to produce a genuine random number. Data on the network, keystrokes, mouse movements, etc., are examples of external sources.

If the application is dependent on randomness (e.g. digital roulette wheels), we do not require actual random numbers.

We will implement pseudo random numbers in Numpy random in this article.

According to Numpy Random, Numpy provides a random module to interact with random numbers.

From 0 to 50, produces a random integer number:

#### Example:

from numpy import random mrx = random.randint(50) print(mrx)

Provides a random integer number between 0 and 900:

#### Example:

from numpy import random mrx = random.randint(900) print(mrx)

## Generating NumPy Random Float

Numpy random rand() method produces a random float number between 0 and 1.

Create a random floating number between 0 and 1 as follows:

#### Example:

from numpy import random mrx = random.rand() print(mrx)

From 0 to 1, produce two random floating numbers:

#### Example:

from numpy import random mrx = random.rand(2) print(mrx)

## Generating Random Array

The two methods from the above examples can be utilized to create random arrays in NumPy.

### Integers

With randint(), you can indicate the shape of an array through the size parameter.

Make a 1-D array of 3 random integers in the range of 0 and 250:

#### Example:

from numpy import random mrx=random.randint(250, size=(3)) print(mrx)

Create a 1-D array of 7 random integers ranging from 0 to 820:

#### Example:

from numpy import random mrx=random.randint(820, size=(7)) print(mrx)

Create a 2-D array with 2 rows holding 3 random integer numbers from 0 to 250:

#### Example:

from numpy import random mrx=random.randint(250, size=(2, 3)) print(mrx)

A 2-D array of 4 rows with 7 random integers between 0 and 820 should be created as follows:

#### Example:

from numpy import random mrx=random.randint(820, size=(4, 7)) print(mrx)

### Floats

You can also indicate the array’s shape by utilizing the rand() method.

Make a 1-D array including three random floats:

#### Example:

from numpy import random mrx = random.rand(3) print(mrx)

Create a 1-D array of 7 random floating numbers:

#### Example:

from numpy import random mrx = random.rand(7) print(mrx)

Create a 2-D array with 2 rows holding 3 random floating numbers:

#### Example:

from numpy import random mrx = random.rand(2, 3) print(mrx)

A 2-D array of 4 rows with 7 random floating numbers must be generated as follows:

#### Example:

from numpy import random mrx=random.rand(4, 7) print(mrx)

## Generating Random Number From Array

By calling the choice() method, you can produce a random item from an array of items.

As a parameter, choice() method provides a random item from an array.

In an mrx array, provide one of the following values:

#### Example:

from numpy import random mrx = random.choice([65, 8 , 12, 33, 19, 83, 77]) print(mrx)

Retrieve an element from the primeNum array:

#### Example:

from numpy import random primeNum = random.choice([2,3,5,7,11,13,17,19]) print(primeNum)

Choose multiple values using the choice() method!

Include a size parameter to determine the shape of the array.

Create a 2-D array with the elements 4, 9, 16, 25, 36, 49, 64 and 82:

#### Example:

from numpy import random square = random.choice([4, 9, 16, 25, 36, 49, 64, 82], size=(2, 3)) print(square)

Make a 2-D array with the following items: ‘161’, ‘28582’, ‘363’, ‘71517’, ‘101’, ‘010’, ‘31113’, ‘90209’ and ‘494’:

#### Example:

from numpy import random palindrome = random.choice(['161', '28582', '363', '71517', '101', '010', '31113', '90209', '494'], size=(2, 3)) print(palindrome)

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!