Generating Random Numbers and Arrays in Matlab and Numpy | by Xinyu Chen (陈新宇) | Towards Data Science

As we know, Numpy is a famous Python library for supporting matrix computations in Python, along with a large collection of high-level mathematical functions to operate on vector, matrix, and tensor arrays [1]. Matlab is a programming language and numerical computing environment, supporting matrix computations, plotting of functions and data, implementation of algorithms, creation of interfaces, and interfacing with programs written in other languages [2].
A common feature of Numpy and Matlab is their broad use in matrix computations and numerical computing. However, they are not completely the same. In this post, we will discuss how to generate random numbers and arrays in both Matlab and Numpy. In particular, we make a comparison between Matlab and Numpy for reproducing the same random numbers and arrays.
Both Matlab and Numpy can generate random numbers and arrays according to a certain command. There are two critical concepts:
Random number distribution
Random seed
For random number distributions, we usually take into account uniform distribution and standard normal (Gaussian) distribution. In Numpy, we can use numpy.random.rand to generate uniform-distributed random numbers. In Matlab, we can use rand to generate uniform-distributed random numbers. Here are some simple examples:
Numpy in Python:
import numpy as npsample = np.random.rand(2)print(sample)
The output is:
[0.70573498 0.8595017 ]
Matlab:
sample = rand(1, 2)
The output is:
ans =   0.5390   0.7686
Above examples are about uniform-distributed random numbers. We can generate random numbers that follow the standard normal distribution:
Numpy in Python: np.random.randn
Matlab: randn
It is possible to use a certain command/function to generate random numbers following one specific distribution, but every time we reproduce our codes, the generated random number would be different. Therefore, random seed is another important concept, which allows one to fix the random numbers in each time.
For example, the following Python code:
import numpy as npnp.random.seed(10)sample = np.random.rand(2)print(sample)
would produce these random numbers:
[0.77132064 0.02075195]
In Matlab:
rng(10);sample = rand(1, 2)
we have the following output:
sample =0.7713    0.0208
In both Numpy and Matlab, they can handle computations of different arrays:
1-D array (one-dimensional array) refers to vector.
2-D array (two-dimensional array) refers to matrix.
3-D array (three-dimensional array) refers to third-order tensor.
......
n-D array (n-dimensional array) refers to nth-order tensor.
In what follows, we will try to generate uniform-distributed random numbers in Numpy and Matlab, respectively. We will also try to generate same random numbers in Numpy and Matlab.
To get the same uniform-distributed random numbers in Numpy and Matlab, we set the vector size as 4 and the random seed as 10. Let us try it!
Numpy:
import numpy as npnp.random.seed(10)sample = np.random.rand(4)print(sample)
Run it, we have
[0.77132064 0.02075195 0.63364823 0.74880388]
Matlab:
rng(10);sample = rand(1, 4)
Run it, we have
sample =    0.7713    0.0208    0.6336    0.7488
It is obvious that both two generated random vectors are same.
First, we set the matrix size as 2-by-3 and the random seed as 10.
Numpy:
import numpy as npnp.random.seed(10)sample = np.random.rand(2, 3)print(sample)
Run it, we have
[[0.77132064 0.02075195 0.63364823] [0.74880388 0.49850701 0.22479665]]
Matlab:
rng(10);sample = rand(2, 3)
Run it, we have
sample =    0.7713    0.6336    0.4985    0.0208    0.7488    0.2248
The generated uniform-distributed random matrices are same.
First, we set the matrix size as 2-by-3-by-2 and the random seed as 10.
Numpy:
import numpy as npnp.random.seed(10)sample = np.random.rand(2, 3, 2)print(‘sample[:, :, 0] = ‘)print()print(sample[:, :, 0])print()print(‘sample[:, :, 1] = ‘)print()print(sample[:, :, 1])
Run it, we have
sample[:, :, 0] = [[0.77132064 0.63364823 0.49850701] [0.19806286 0.16911084 0.68535982]]sample[:, :, 1] = [[0.02075195 0.74880388 0.22479665] [0.76053071 0.08833981 0.95339335]]
Matlab:
rng(10);sample = rand(2, 3, 2)
Run it, we have
sample(:,:,1) =    0.7713    0.6336    0.4985    0.0208    0.7488    0.2248sample(:,:,2) =    0.1981    0.1691    0.6854    0.7605    0.0883    0.9534
The generated uniform-distributed random tensors are same.
In above, Numpy and Matlab can generate same uniform-distributed random numbers if we use the same random seed. Unfortunately, since Numpy and Matlab use different transformations to generate samples from the standard normal distribution, we therefore need to use the same transformation in both Numpy and Matlab.
In practice, it is still possible to reproduce the output of randn() of Matlab as same as Numpy.
Numpy:
import numpy as npfrom scipy.stats import normnp.random.seed(10)sample = norm.ppf(np.random.rand(2, 3))print(sample)
Run it, we have
[[ 0.74320312 -2.03846052  0.34153145] [ 0.67073049 -0.00374237 -0.75609325]]
Matlab:
rng(10);sample = norminv(rand(2, 3), 0, 1)
Run it, we have
sample =    0.7432    0.3415   -0.0037   -2.0385    0.6707   -0.7561
The generated normal-distributed random matrices are same.
[1] Numpy on Wikipedia: https://en.wikipedia.org/wiki/NumPy
[2] Matlab on Wikipedia: https://en.wikipedia.org/wiki/MATLAB
[3] Numpy logo is from https://commons.wikimedia.org/wiki/File:NumPy_logo_2020.svg
[4] Matlab logo is from https://www.google.com/url?esrc=s&q=&rct=j&sa=U&url=https://1000logos.net/matlab-logo/&ved=2ahUKEwj24b7C7bvzAhUKoXIEHci3AdQQr4kDegQIEhAC&usg=AOvVaw2JKOiqdedKhhEEDnpm7AcW
[5] Reproducing Random Numbers in Matlab and Python / NumPy. GitHub website: https://github.com/jonasrauber/randn-matlab-python
[6] Is it possible to reproduce randn() of MATLAB with NumPy? https://stackoverflow.com/questions/3722138/is-it-possible-to-reproduce-randn-of-matlab-with-numpy
[7] Comparing Matlab and Numpy code that uses random number generation. https://stackoverflow.com/questions/18486241/comparing-matlab-and-numpy-code-that-uses-random-number-generation