How to create a matrix of random integers in Python ? Last Updated : 23 Jul, 2025 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 Company-wise Practice Problems S shilpimazumdar7150 Follow Improve Article Tags : Python Python-numpy Python numpy-Matrix Function Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like