Random Generators in Numpy

Anant Dhakad
Last Updated: May 13, 2022

Introduction

A random number does not always equal a different number. Something that cannot be predicted logically is referred to as random.

 

Pseudo-Random and True Random

Computers run programs, which are a set of instructions that must be followed precisely. As a result, some algorithms for generating a random number must exist.

Because it is possible to predict a random number generated by software, it is not truly random. Pseudorandom numbers are those formed using a generating algorithm.

 

Functions in NumPy Random

  1. np.random.random() - This function returns a random number between 0 and 1.
  2. np.random.randint() - This function returns a random number between specified ranges.
  3. np.random.choice() - This function returns random elements from an array.
  4. np.random.random_sample() - This function is one of the NumPy functions for random sampling.
  5. np.random.uniform() - This function returns a NumPy array of random integers within a given range.
  6. np.random.seed() 
  7. np.random.normal() -  Using this function you can declare a NumPy array that stores properly dispersed data with this function(normally distributed data).
  8. np.random.default_rng() 

 

np.random.random()

In NumPy, one of the functions for random sampling is numpy.random.random(). It fills an array with random floats in the half-open interval [0.0, 1.0] and returns an array of the desired shape.

syntax :- numpy.random.random(size=None)

Example

import numpy as np

_num = np.random.random()
print("Random number : ", _num)

arr = np.random.random((2, 2))
print("arr : ", arr)

Output

Random number :  0.9356141266366734
arr :  [[0.25211455 0.81687095]
        [0.51159055 0.46349961]]

 

np.random.randint()

In NumPy, one of the functions for random sampling is numpy.random.randint(). It returns an array of the requested shape, which is filled with random integers ranging from low (inclusive) to high (exclusive), i.e., in the range [low, high].

syntax :- numpy.random.randint(low, high=None, size=None, dtype='l')

Example 

import numpy as np

_num = np.random.randint(4)
print("Random number : ", _num)

_num = np.random.randint(4, 6)
print("Random number in range : ", _num)

# Creating random array using randint()
arr = np.random.randint(20, size = 4)
print("Random Array : ", arr)

Output

Random number :  1
Random number in range :  4
Random Array :  [ 6  3 11  5]

 

np.random.choice()

We can retrieve random samples from a one-dimensional array and return random samples from a NumPy array using the choose() method.

syntax :- numpy.random.choice(a, size=None, replace=True, p=None)

Example

import numpy as np

# Choosing random element from an array
_num = np.random.choice([1, 10, 20, 30])
print("Random element from an array : ", _num)

# Using the random choose() method to generate a random sample
newArr = np.random.choice(10, 2)
print("Random sample : ", newArr)

Output

Random element from an array :  30
Random sample :  [2 5]

 

 

np.random.random_sample()

 

syntax :- numpy.random.random_sample(size=None)

Example

import numpy as np

arr = np.random.random_sample(4)  
print("Random Sample : ", arr)

Output

Random Sample :  [0.84933849 0.5818701  0.39682942 0.66025904]

 

np.random.uniform()

We may obtain random samples from a uniform distribution using the numpy.random.uniform() method, which returns the random samples as a NumPy array.

 

syntax :- numpy.random.uniform(low=0.0, high=1.0, size=None)

Example

import numpy as np

uniformArray = np.random.uniform(0, 2, 5)
print("Uniform Sample : ", uniformArray)

Output

Uniform Sample :  [1.24454379 0.55701666 0.61038106 1.29815877 1.31664873]

 

np.random.seed()

The seed function saves the state of a random function to create the same random numbers on repeated executions of the code on the same or other machines (for a specific seed value). The previous value number created by the generator is the seed value. When there is no previous value, it uses the current system time for the first time.

 

syntax :- numpy.random.seed(seed=None)

Example

import numpy as np

np.random.seed(0)
num1 = np.random.random()
print("num1 : ", num1)

np.random.seed(0)
num2 = np.random.random()
print("num2 : ", num2)

Output

num1 :  0.5488135039273248
num2 :  0.5488135039273248

 

np.random.normal()

This function constructs an array of the requested shape and fills it with random values from the Normal(Gaussian) Distribution. Because of its distinctive shape, this distribution is also known as the Bell Curve.

 

syntax :- numpy.random.normal(loc = 0.0, scale = 1.0, size = None)

Example 

import numpy as np

arr = np.random.normal(size=4)
print("Array : ", arr)

Output 

Array :  [ 0.74159174  1.55291372 -2.2683282   1.33354538]

 

np.random.default_rng()

The generator is replaced with a random state in Python and allows access to a large range of normal distributions. It is a mathematical method for generating a sequence of almost random numbers, with an auxiliary bit generator controlling random bit numbers. The random values in Python are generated by the generator and come from a Bit generator.

 

Example

import numpy as np

rng = np.random.default_rng(6789)
print(rng)

num = rng.random()
print("num : ", num)

Output

Generator(PCG64)
num :  0.5555223396843776

 

FAQs

  1. List the functions in the random class of the NumPy library.
    There are many function in the random class such as random(), choice(), randint() etc. For an extensive list refer to the official doc of NumPy.
     
  2. How to generate a random float number using NumPy?
    Random float numbers can be generated using the rand() function.
     
  3. How to choose a random number from an array or list in Python?
    Random elements from an array can be chosen using the choice() function.
     
  4. Explain the random permutation in NumPy.
    We can get random samples of permutation sequences and return sequences using the numpy.random.permutation() method.

Key Takeaways

For more detailed descriptions, refer to NumPy’s official documentation.

Cheers if you reached here!! 

Yet learning never stops, and there is a lot more to learn. Happy Learning!!

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think