# NumPy Random

## Codes With Pankaj

# Introduction

we’ll explore the concept of random numbers in Python and learn how to generate random integers, floats, and arrays using the NumPy library. We’ll cover both pseudo-random numbers and truly random numbers, emphasizing the practical use of pseudo-random numbers in most applications.

# What is a Random Number?

A random number is not necessarily different every time; rather, it means something that cannot be predicted logically. In the context of computers, which operate based on programs and algorithms, true randomness is challenging to achieve. Instead, we often work with pseudo-random numbers, generated through algorithms that produce seemingly random sequences.

# Pseudo Random and True Random

Computers use algorithms to generate random numbers, making them predictable. Such numbers are termed pseudo-random. True randomness, on the other hand, involves obtaining data from an external source, such as keystrokes, mouse movements, or network data. While true random numbers have specific applications, many scenarios, including most programming tasks, can be effectively addressed using pseudo-random numbers.

# Generating Pseudo-Random Numbers with NumPy

NumPy, a powerful numerical computing library in Python, provides a `random`

module for working with random numbers.

# Generate Random Integer

To generate a random integer between 0 and 100, we can use the `randint()`

method.

`from numpy import random`

x = random.randint(100)

print(x)

# Generate Random Float

To generate a random float between 0 and 1, we use the `rand()`

method.

`from numpy import random `

x = random.rand()

print(x)

# Generate Random Array

NumPy allows us to generate random arrays of integers or floats.

## Random Integer Array

To create a 1-D array with random integers, we use `randint()`

with the `size`

parameter.

`from numpy import random`

x = random.randint(100, size=(5))

print(x)

To generate a 2-D array, we adjust the `size`

parameter accordingly.

`from numpy import random`

x = random.randint(100, size=(3, 5))

print(x)

## Random Float Array

For random float arrays, we use the `rand()`

method similarly.

`from numpy import random`

x = random.rand(5)

print(x)

Adjusting the `size`

parameter allows us to create 2-D float arrays as well.

`from numpy import random`

x = random.rand(3, 5)

print(x)

# Generate Random Number From Array

The `choice()`

method enables us to generate random values based on an array of values.

## Single Value

`from numpy import random`

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

print(x)

## Array of Values

To generate a 2-D array with random values from a given array, we use the `size`

parameter.

`from numpy import random`

x = random.choice([3, 5, 7, 9], size=(3, 5))

print(x)

# Random Data Distribution

# Random Distribution

A random distribution is a set of random numbers that follows a specific probability density function. The probability density function describes the likelihood of each value occurring in the distribution. NumPy’s `random`

module provides the `choice()`

method, allowing us to generate random numbers based on defined probabilities.

# Generating Random Data Distribution

# Example: Generating a 1-D Array

Let’s generate a 1-D array containing 100 values, where each value can be 3, 5, 7, or 9. We’ll set the probability for each value using the `p`

parameter in the `choice()`

method.

`from numpy import random`

# Define values and their probabilities

values = [3, 5, 7, 9]

probabilities = [0.1, 0.3, 0.6, 0.0]

# Generate a 1-D array with 100 values based on probabilities

x = random.choice(values, p=probabilities, size=(100))

print(x)

In this example, the probability for the value 3 is set to 0.1, for 5 is set to 0.3, for 7 is set to 0.6, and for 9 is set to 0.0. The sum of all probability values should be 1. The resulting array represents a random distribution based on the defined probabilities.

# Example: Generating a 2-D Array

We can extend the example to generate a 2-D array with multiple rows and columns.

`from numpy import random`

# Define values and their probabilities

values = [3, 5, 7, 9]

probabilities = [0.1, 0.3, 0.6, 0.0]

# Generate a 2-D array with 3 rows and 5 columns based on probabilities

x = random.choice(values, p=probabilities, size=(3, 5))

print(x)

Adjust the `size`

parameter to control the shape of the array. In this case, we have 3 rows and 5 columns. The resulting 2-D array reflects the random distribution specified by the given probabilities.

# Random Permutations

# Shuffling Arrays

Shuffling means changing the arrangement of elements in-place, directly within the array. The `shuffle()`

method in the NumPy `random`

module allows us to achieve this.

## Example: Shuffling an Array

Let’s randomly shuffle the elements of an array using the `shuffle()`

method.

`from numpy import random`

import numpy as np

# Create an array

arr = np.array([1, 2, 3, 4, 5])

# Shuffle the array in-place

random.shuffle(arr)

# Print the shuffled array

print(arr)

The `shuffle()`

method modifies the original array, changing the order of its elements.

# Generating Permutations of Arrays

The `permutation()`

method in the NumPy `random`

module generates a random permutation of the elements in an array.

## Example: Generating a Random Permutation

Let’s generate a random permutation of the elements in an array using the `permutation()`

method.

`from numpy import random`

import numpy as np

# Create an array

arr = np.array([1, 2, 3, 4, 5])

# Generate a random permutation of the array

permuted_arr = random.permutation(arr)

# Print the random permutation

print(permuted_arr)

The `permutation()`

method returns a new array representing a random permutation of the elements in the original array. The original array remains unchanged.