Python numpy random randint

The Python numpy random randint function returns or generates random integer values from the start (low) to end (high). The syntax of the numpy random randint function is

numpy.random.randint(low, high = None, size  = None, dtype = int)
  • low: Lowest integer drew from the distribution. If the high value is not None, it works as the starting value, and the high is None, then it acts as the highest value.
  • high: It is optional, and this is the highest integer to be drawn from the distribution. 
  • size: It can be integer or tuple of int values. Here, we have to specify the array size or dimension. For instance, size = (2, 3) fills two rows and three columns array with random integer values.
  • dtype: Data type of the output.

Python numpy random randint Examples

The numpy random randint function returns the discrete uniform distribution random integers between low (inclusive) and high (exclusive). If we don’t specify the size, then it returns a single value. The below example prints the random number between 0 and 3.

import numpy as tg

randintArr = tg.random.randint(3)

print(randintArr)
1

Numpy random randint 1D Arrays

In this example, we used the low and size parameter to return the one-dimensional random integer array. It means the low value becomes the high value in the absence of a high parameter. Here, tg.random.randint(3, size = 5) returns an array of five items with random values less than three. Next, tg.random.randint(7, size = 10) returns an array of ten random values less than 7.

import numpy as tg

randintArr1 = tg.random.randint(3, size = 5)

print(randintArr1)

randintArr2 = tg.random.randint(7, size = 10)

print(randintArr2)
[1 0 2 2 2]
[6 0 0 4 2 5 1 1 5 2]

In this example, we assigned the low and high values. It means the first statement returns the array of eight random numbers between 3 and 10. 

import numpy as tg

randintArr1 = tg.random.randint(3, 10, size = 8)

print(randintArr1)

randintArr2 = tg.random.randint(10, 50, size = 10)

print(randintArr2)
[7 8 6 9 5 8 4 5]
[38 14 25 23 17 26 37 12 47 20]

Numpy random randint 2D Arrays

In this example, the first two random randint statements print the two-dimensional array with default lower limit and given high limit. The other two statements return a 2D random number array from given low to high value. tg.random.randint(20, 90, size = (3, 6)) returns a 3 * 6 matrix of random integers between 20 and 90.

import numpy as tg

randintArr1 = tg.random.randint(5, size = (2, 3))
print(randintArr1)

randintArr2 = tg.random.randint(12, size = (4, 6))
print(randintArr2)

randintArr3 = tg.random.randint(20, 90, size = (3, 6))
print(randintArr3)

randintArr4 = tg.random.randint(9, 49, size = (5, 7))
print(randintArr4)
Python numpy random randint 1

Numpy random randint 3D Array

It will generate the three dimensional array of random numbers using the Python numpy random randint.

import numpy as tg

drandintArr1 = tg.random.randint(8, size = (2, 2, 3))
print(drandintArr1)

drandintArr2 = tg.random.randint(22, size = (2, 3, 5))
print(drandintArr2)

drandintArr3 = tg.random.randint(129, 430, size = (3, 3, 5))
print(drandintArr3)

drandintArr4 = tg.random.randint(111, 149, size = (2, 4, 7))
print(drandintArr4)
[[[3 5 0]
  [7 4 4]]

 [[6 6 6]
  [2 5 7]]]
[[[14  2  0  1 12]
  [11 20 17 17 19]
  [14 18  4  4 17]]

 [[ 0 19  8  6  0]
  [ 4  7  2 11 17]
  [18  3 18 12  6]]]
[[[142 178 428 360 192]
  [327 232 198 309 175]
  [272 181 370 223 380]]

 [[360 402 282 377 213]
  [164 250 146 212 243]
  [151 255 406 184 172]]

 [[153 360 324 285 200]
  [359 220 298 214 411]
  [245 409 228 400 282]]]
[[[137 145 144 133 135 114 141]
  [126 144 147 141 115 134 133]
  [131 121 115 122 130 146 124]
  [148 120 131 120 125 136 136]]

 [[131 129 141 129 116 132 126]
  [145 124 120 139 141 139 123]
  [140 148 145 119 113 134 116]
  [111 126 135 122 144 137 114]]]

numpy random randint with all parameters

import numpy as tg

randintArr1 = tg.random.randint(5, high = 20, size = (2, 3), dtype = tg.int16)
print(randintArr1)

randintArr2 = tg.random.randint(99, 159, size = (2, 3, 5), dtype = tg.int16)
print(randintArr2)
[[ 6  9 10]
 [19  5 18]]
[[[139 149 101 148 150]
  [153 147 158 106 126]
  [152 112 102 134 150]]

 [[138 130 108 153 133]
  [106 107 136 119 124]
  [125 110 142 103 118]]]

The numpy random randint function also allows using multiple values as the lower ad higher distribution values. Here, tg.random.randint([1, 3, 9], 22) returns a random value between 1 and 22, 3 and 22, 9 and 22. Next, tg.random.randint(7, [22, 44, 99]) returns a random value between 7 and 22, 7 and 44, 7 and 99.

import numpy as tg

randintArr1 = tg.random.randint([1, 3, 9], 22)
print(randintArr1)

randintArr2 = tg.random.randint([11, 3, 99, 9], 122)
print(randintArr2)

randintArr3 = tg.random.randint(7, [22, 44, 99])
print(randintArr3)

randintArr4 = tg.random.randint(99, [122, 233, 999, 333, 194])
print(randintArr4)
[14 11 21]
[ 89 113 100  38]
[11 14 62]
[120 156 937 260 125]

Apart from that, we can also use the nested value to create a multidimensional array of random integer values between the low and high.

import numpy as tg

randintArr1 = tg.random.randint([4, 3, 7], [[10], [20]])
print(randintArr1)

print("====Second Array====")
randintArr2 = tg.random.randint([99, 3, 99, 9], [[189], [222]])
print(randintArr2)

print("====Third Array====")
randintArr3 = tg.random.randint([[56], [75]], [129, 264, 199])
print(randintArr3)

print("====Fourth Array====")
randintArr4 = tg.random.randint([[99], [122], [233]], [999, 443, 394])
print(randintArr4)
[[ 8  7  9]
 [ 8 14 12]]
====Second Array====
[[130  21 167  69]
 [189 155 201  56]]
====Third Array====
[[ 93 230  70]
 [ 86 212 147]]
====Fourth Array====
[[981 342 380]
 [903 176 325]
 [862 252 271]]