Python numpy random randint

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

The syntax of the Python numpy random randint function is

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

Python numpy random randint Examples

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

import numpy as tg

Arr = tg.random.randint(3)

print(Arr)
1

Python 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, (3, size = 5) returns an array of five items with values less than three. Next, (7, size = 10) returns an array of ten values less than 7.

import numpy as tg

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

print(a)

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

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

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

import numpy as tg

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

print(c)

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

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

Python numpy random randint 2D Arrays

In this example, the first two statements print the two-dimensional array with a default lower limit and a given high limit. The other two statements return a 2D random number array from low to high values. For instance, (20, 90, size = (3, 6)) returns a 3 * 6 matrix of 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

3D Array

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

import numpy as tg

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

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

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

d = tg.random.randint(111, 149, size = (2, 4, 7))
print(d)
[[[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]]]

randint with all parameters

import numpy as tg

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

f = tg.random.randint(99, 159, size = (2, 3, 5), dtype = tg.int16)
print(f)
[[ 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]]]

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

import numpy as tg

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

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

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

j = tg.random.randint(99, [122, 233, 999, 333, 194])
print(j)
[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 integer values between the low and high.

import numpy as tg

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

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

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

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