How to create a matrix of random integers in Python ? (original) (raw)
Last Updated : 11 Dec, 2020
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
import
numpy as np
array
=
np.random.randint(
10
, size
=
(
20
))
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
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]]
Similar Reads
- How to Create a List of N-Lists in Python In Python, we can have a list of many different kinds, including strings, numbers, and more. Python also allows us to create a nested list, often known as a two-dimensional list, which is a list within a list. Here we will cover different approaches to creating a list of n-lists in Python. The diffe 3 min read
- How to Pick a Random Card in Python The Python Random module is a built-in Python module, used to create random integers. Because these are pseudo-random numbers, they are not actually random. This module may be used to generate random numbers, print a random value from a list or string, and so on. To choose a random card from a deck 3 min read
- 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 generate a random color for a Matplotlib plot in Python? Handling huge dataset requires libraries and methods that can be used to store, analyze and manipulate the datasets. Python is a popular choice when it comes to data science and analytics. Data scientists prefer Python due to its wide library support that contains functions which can be used to work 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 phone number using Python? In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with 1 min read
- Create a Random Password Generator using Python In this article, we will see how to create a random password generator using Python. Passwords are a means by which a user proves that they are authorized to use a device. It is important that passwords must be long and complex. It should contain at least more than ten characters with a combination 5 min read
- Calculate standard deviation of a Matrix in Python In this article we will learn how to calculate standard deviation of a Matrix using Python. Standard deviation is used to measure the spread of values within the dataset. It indicates variations or dispersion of values in the dataset and also helps to determine the confidence in a model’s statistica 2 min read