Python | Generate Random Numbers Using NumPy Library

Random Numbers using NumPy: In this tutorial, we will learn how to generate random numbers using NumPy methods? By Ritik Aggarwal Last updated : April 14, 2023

Introduction

The purpose of this tutorial is to learn how can we generate the random numbers using the different methods of Python's numpy library?

Random Number Generation: Random number generation in very important in the field of machine learning. It is used to initialize weights in almost every machine learning algorithm.

Different Methods to Generate Random Numbers

The following are the NumPy library methods to generate random numbers:

1. numpy.random.rand() Method

It take shape of the array as its argument and then generate random numbers and fill whole the array with the random numbers that lies in between 0 and 1. The distribution of random numbers follows uniform distribution.

Syntax

random.rand(d0, d1, ..., dn)

Example

import numpy as np

A = np.random.rand(2,5)
print(A)

"""
Output:
[[0.75331495 0.53453136 0.09320152 0.65159053 0.25946584]
 [0.11214697 0.25152484 0.53601778 0.31374353 0.22139282]]
""" 

2. numpy.random.randint() Method

It takes two arguments(low and high). It generates random integer between low and high in which low is inclusive and high is exclusive. It follows discrete uniform distribution.

Syntax

random.randint(low, high=None, size=None, dtype=int)

Example

import numpy as np

B = np.random.randint(2,17)
print(B)

"""
Output:
12
""" 

3. numpy.random.randn() Method

It takes shape of the array as its argument and generate random numbers in the form of gaussian distribution with mean as 0 and variance as 1. It follows standard normal distribution.

Syntax

random.randn(d0, d1, ..., dn)

Example

import numpy as np

C = np.random.randn(2,5)
print(C)

"""
Output:
[[-0.66890992  0.13376387 -0.66919788 -0.13076927 -0.02596615]
 [ 0.23429437  0.63925292  0.88980204 -0.54101789  0.56826184]]
""" 

4. numpy.random.random() Method

It takes size as its argument and generate random number random number lying between 0 and 1. It follows continuous random distribution.

Syntax

random.random(size=None)

Example

import numpy as np

D = np.random.random((2,5))
print(D)

"""
Output:
[[0.91217716 0.7055205  0.06214276 0.12826761 0.84553533]
 [0.14169982 0.46552049 0.67407509 0.07996351 0.64416007]]
""" 

5. numpy.random.multivariate_normal() Method

It primarily takes three arguments(mean of individual feature in form of matrix, co -variance matrix and last argument is number of datapoints). For generating data for more than one feature, mean and variance matrix must be of higher dimension. It follows multivariate normal distribution.

Syntax

random.multivariate_normal(mean, cov, size=None, check_valid='warn', tol=1e-8)

Example

import numpy as np

mean = [1, 2]
matrix = [[5, 0], [0, 5]]
E = np.random.multivariate_normal(mean, matrix, 10)
print(E)

"""
Output:
[[ 3.71923938  1.92105736]
 [ 1.95423236  3.34892689]
 [ 1.6581533  -1.75526329]
 [-3.32672249  1.39740772]
 [ 0.53979176  1.11019364]
 [ 2.00899418  0.29714746]
 [-0.15685733 -0.09417999]
 [ 0.78637943  1.58751813]
 [ 3.76994611  3.62513883]
 [-0.96897309  0.80375726]]
""" 

Python Code for Generate Random Numbers Using NumPy Library

import numpy as np

print("Using random.rand() Method")
A = np.random.rand(2,5)
print(A)

print("Using random.randint() Method")
B = np.random.randint(2,17)
print(B)

print("Using random.randn() Method")
C = np.random.randn(2,5)
print(C)

print("Using random.random() Method")
D = np.random.random((2,5))
print(D)

print("Using random.multivariate_normal() Method")
mean = [1, 2]
matrix = [[5, 0], [0, 5]]
E = np.random.multivariate_normal(mean, matrix, 10)
print(E)

Output

Using random.rand() Method
[[0.78385445 0.2289615  0.09114823 0.2191635  0.74697731]
 [0.31164242 0.71479646 0.28864044 0.32429063 0.63021416]]
Using random.randint() Method
12
Using random.randn() Method
[[ 0.45734685 -0.17513153  0.92153419  0.05963536  0.71238261]
 [ 2.51500105 -1.6410566   1.39092351  2.51919212 -0.4558811 ]]
Using random.random() Method
[[0.45073473 0.30008836 0.51830218 0.64515255 0.42853555]
 [0.84681976 0.30208051 0.2464953  0.7344328  0.74286827]]
Using random.multivariate_normal() Method
[[-0.51662975 -0.66572196]
 [ 1.42510487  2.50190297]
 [ 1.94856269  0.02482849]
 [-0.99670471  1.62482575]
 [ 1.11578151  0.36340097]
 [-0.54523713  3.77938918]
 [ 1.25758145  3.38103955]
 [ 0.94582942  2.40589228]
 [ 0.99165433 -1.09521125]
 [ 0.17911194 -0.41636468]]
 

Python Basic Programs »


Related Programs



All Python NumPy Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.