Table of contents
1.
Introduction
2.
Pseudo-Random and True Random
3.
Functions in NumPy Random
4.
np.random.random()
5.
np.random.randint()
6.
np.random.choice()
7.
np.random.random_sample()
8.
np.random.uniform()
9.
np.random.seed()
10.
np.random.normal()
11.
np.random.default_rng()
12.
FAQs
13.
Key Takeaways
Last Updated: Mar 27, 2024

Random Generators in Numpy

Author Anant Dhakad
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)
You can also try this code with Online Python Compiler
Run Code

Example

import numpy as np

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

arr = np.random.random((2, 2))
print("arr : ", arr)
You can also try this code with Online Python Compiler
Run Code

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')
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Output

Random number :  1
Random number in range :  4
Random Array :  [ 6  3 11  5]
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Output

Random element from an array :  30
Random sample :  [2 5]
You can also try this code with Online Python Compiler
Run Code

 

 

np.random.random_sample()

 

syntax :- numpy.random.random_sample(size=None)
You can also try this code with Online Python Compiler
Run Code

Example

import numpy as np

arr = np.random.random_sample(4)  
print("Random Sample : ", arr)
You can also try this code with Online Python Compiler
Run Code

Output

Random Sample :  [0.84933849 0.5818701  0.39682942 0.66025904]
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

Example

import numpy as np

uniformArray = np.random.uniform(0, 2, 5)
print("Uniform Sample : ", uniformArray)
You can also try this code with Online Python Compiler
Run Code

Output

Uniform Sample :  [1.24454379 0.55701666 0.61038106 1.29815877 1.31664873]
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Output

num1 :  0.5488135039273248
num2 :  0.5488135039273248
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

Example 

import numpy as np

arr = np.random.normal(size=4)
print("Array : ", arr)
You can also try this code with Online Python Compiler
Run Code

Output 

Array :  [ 0.74159174  1.55291372 -2.2683282   1.33354538]
You can also try this code with Online Python Compiler
Run Code

 

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)
You can also try this code with Online Python Compiler
Run Code

Output

Generator(PCG64)
num :  0.5555223396843776
You can also try this code with Online Python Compiler
Run Code

 

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!!

Live masterclass