How to create matrix of random numbers in Python – NumPy
This Python tutorial will focus on how to create a random matrix in Python. Here we will use NumPy library to create matrix of random numbers, thus each time we run our program we will get a random matrix.
We will create these following random matrix using the NumPy library.
- Matrix with floating values
- Random Matrix with Integer values
- Random Matrix with a specific range of numbers
- Matrix with desired size ( User can choose the number of rows and columns of the matrix )
Create Matrix of Random Numbers in Python
We will create each and every kind of random matrix using NumPy library one by one with example. Let’s get started.
To perform this task you must have to import NumPy library. The below line will be used to import the library.
import numpy as np
Note that np is not mandatory, you can use something else too. But it’s a better practice to use np.
Here are some other NumPy tutorials which you may like to read.
- How to convert a float array to int in Python – NumPy
- How to create 2D array from list of lists in Python
Random 1d array matrix using Python NumPy library
import numpy as np random_matrix_array = np.random.rand(3) print(random_matrix_array)
$ python codespeedy.py [0.13972036 0.58100399 0.62046278]
The elements of the array will be greater than zero and less than one.
2d matrix using np.random.rand()
import numpy as np random_matrix_array = np.random.rand(3, 4) print(random_matrix_array)
[[0.43189018 0.0903101 0.2664645 0.37512746] [0.63474244 0.91995859 0.84270619 0.97062349] [0.19307901 0.29623444 0.30945273 0.93585395]]
Create a 3D matrix of random numbers in Python
import numpy as np random_3d_matrix_array = np.random.rand(3, 4, 2) print(random_3d_matrix_array)
[[[0.55267301 0.95526256] [0.92689674 0.86599548] [0.87304883 0.32868337] [0.14190636 0.92375264]] [[0.22447201 0.00706627] [0.60944606 0.71169812] [0.371652 0.48960865] [0.77221671 0.30692933]] [[0.11237068 0.99828592] [0.1608211 0.47616887] [0.5892122 0.52634281] [0.10034398 0.36586993]]]
np.random.rand() to create random matrix
All the numbers we got from this np.random.rand() are random numbers from 0 to 1 uniformly distributed. You can also say the uniform probability between 0 and 1.
- Parameters: It has parameter, only positive integers are allowed to define the dimension of the array. If you want to create a 1d array then use only one integer in the parameter. To make a 2d array matrix put 2 integers. The first integer is the number of rows and the 2nd one is the number of columns.
- Return Type: ndarray
Create matrix of random integers in Python
In order to create a random matrix with integer elements in it we will use:
Here the default dtype is int so we don’t need to write it.
lowe_range and higher_range is int number we will give to set the range of random integers.
m,n is the size or shape of array matrix. m is the number of rows and n is the number of columns.
higher_range is optional.
Here are a few examples of this with output:
Examples of np.random.randint() in Python
Matrix of random integers in a given range with specified size
import numpy as np random_matrix_array = np.random.randint(1,10,size=(3,4)) print(random_matrix_array)
$ python codespeedy.py [[8 4 7 1] [6 1 9 4] [4 3 3 1]]
Here the matrix is of 3*4 as we defined 3 and 4 in size=()
All the random elements are from 1 to 10 as we defined the lower range as 1 and higher as 10.
Matrix of 0 and 1 randomly
import numpy as np random_matrix_array = np.random.randint(2,size=(3,4)) print(random_matrix_array)
[[1 0 1 1] [1 1 0 1] [1 0 1 1]]
Note that: If you define the parameters as we defined in the above program, the first parameter will be considered as a higher range automatically. And the lower range will be set to zero by default.
Want to create a game with random numbers? follow the below tutorial,