How to create a matrix of random integers in Python ? Last Updated : 11 Dec, 2020 Comments Improve Suggest changes Like Article Like Report Prerequisites: numpy To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand. Syntax : numpy.random.randint(low, high=None, size=None, dtype=’l’) Parameters : low : [int] Lowest (signed) integer to be drawn from the distribution.But, it works as a highest integer in the sample if high=None. high : [int, optional] Largest (signed) integer to be drawn from the distribution. size : [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned. dtype : [optional] Desired output data-type. Return : Array of random integers in the interval [low, high) or a single such random int if size not provided. Example 1: Python3 # importing numpy library import numpy as np # random is a function, doing random sampling in numpy. array = np.random.randint(10, size=(20)) # the array will be having 20 elements. print(array) Output: [2 6 1 4 3 3 6 5 0 3 6 8 9 1 6 4 0 5 4 1] Example 2: Python3 import numpy as np # 1st argument --> numbers ranging from 0 to 9, # 2nd argument, row = 2, col = 3 array = np.random.randint(10, size=(2, 3)) print(array) Output: [[8 6 7] [2 9 9]] Example 3: Python3 import numpy as np array = np.random.randint(2, size=(5, 5)) print(array) Output: [[0 0 1 0 0] [1 0 1 1 0] [0 1 0 1 0] [0 1 0 0 1] [0 1 0 1 0]] Comment More infoAdvertise with us Next Article How to create a matrix of random integers in Python ? S shilpimazumdar7150 Follow Improve Article Tags : Python Python-numpy Python numpy-Matrix Function Practice Tags : python Similar Reads How to randomly insert NaN in a matrix with NumPy in Python ? Prerequisites: Numpy In this article, let's see how to generate a Python Script that randomly inserts Nan into a matrix using Numpy. Given below are 3 methods to do the same: Method 1: Using ravel() function ravel() function returns contiguous flattened array(1D array with all the input-array elemen 3 min read How to create an empty matrix with NumPy in Python? In Python, an empty matrix is a matrix that has no rows and no columns. NumPy, a powerful library for numerical computing, provides various methods to create matrices with specific properties, such as uninitialized values, zeros, NaNs, or ones. Below are different ways to create an empty or predefin 3 min read How to create a constant matrix in Python with NumPy? A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value th 4 min read How to Generate Random Numbers in R Random number generation is a process of creating a sequence of numbers that don't follow any predictable pattern. They are widely used in simulations, cryptography and statistical modeling. R Programming Language contains various functions to generate random numbers from different distributions lik 2 min read How to generate a random number between 0 and 1 in Python ? The use of randomness is an important part of the configuration and evaluation of modern algorithms. Here, we will see the various approaches for generating random numbers between 0 ans 1. Method 1: Here, we will use uniform() method which returns the random number between the two specified numbers 1 min read How to Create Array of zeros using Numpy in Python numpy.zeros() function is the primary method for creating an array of zeros in NumPy. It requires the shape of the array as an argument, which can be a single integer for a one-dimensional array or a tuple for multi-dimensional arrays. This method is significant because it provides a fast and memory 4 min read Python | SymPy combinatorics.random_integer_partition() method With the help of sympy.combinatorics.partitions.random_integer_partition() method, we can get the random partition of n in the form of reversed sorted list by using sympy.combinatorics.partitions.random_integer_partition() method. Syntax : sympy.combinatorics.partitions.random_integer_partition(n) R 1 min read How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read How to randomly select rows of an array in Python with NumPy ? In this article, we will see two different methods on how to randomly select rows of an array in Python with NumPy. Let's see different methods by which we can select random rows of an array: Method 1: We will be using the function shuffle(). The shuffle() function shuffles the rows of an array rand 2 min read How to generate random numbers from a log-normal distribution in Python ? A continuous probability distribution of a random variable whose logarithm is usually distributed is known as a log-normal (or lognormal) distribution in probability theory. A variable x is said to follow a log-normal distribution if and only if the log(x) follows a normal distribution. The PDF is d 3 min read Like