Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 668d7e0

Browse files
authored
randint term added (#7701)
* created the required randint.md file * yarn format * minor fix * created b2a-base16.md * some changes * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Removed unwanted file ---------
1 parent 9a45441 commit 668d7e0

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

  • content/numpy/concepts/random-module/terms/randint
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
Title: 'randint()'
3+
Description: 'Generates random integers within a specified range using NumPy.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Numpy'
9+
- 'Python'
10+
- 'Random'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
**`randint()`** is a function from NumPy's [`random`](https://www.codecademy.com/resources/docs/numpy/random-module) module that generates random integers. It can generate a single integer or an array of integers within a specified range, making it useful for simulations, testing, and randomized operations.
17+
18+
## Syntax
19+
20+
```pseudo
21+
numpy.random.randint(low, high=None, size=None, dtype=int)
22+
```
23+
24+
**Parameters:**
25+
26+
- `low` (int): Lowest (inclusive) integer to be drawn.
27+
- `high` (int, optional): One above the highest integer to be drawn. If not provided, integers are drawn from the range `[0, low)`.
28+
- `size` (int or tuple of ints, optional): Output shape. If `None`, a single integer is returned.
29+
- `dtype` (data-type, optional): Desired data type of the output. Default is `int`.
30+
31+
**Return value:**
32+
33+
- `out` (int or ndarray): Random integer(s) from the specified range.
34+
- If `size` is `None`, returns a single integer.
35+
- If `size` is specified, returns a NumPy array of the given shape.
36+
37+
## Example
38+
39+
This example generates a random integer (0-9) and an array of 5 random integers (1-5):
40+
41+
```py
42+
import numpy as np
43+
np.random.seed(15)
44+
45+
# Single random integer from 0 to 9
46+
single_int = np.random.randint(10)
47+
print(single_int)
48+
49+
# Array of 5 random integers from 1 to 5
50+
arr = np.random.randint(1, 6, size=5)
51+
print(arr)
52+
```
53+
54+
The output for this code will be:
55+
56+
```shell
57+
8
58+
[5 1 5 4 4]
59+
```
60+
61+
> **Note:** Exact output values may vary depending on NumPy version, but the format will be as shown.
62+
63+
Here:
64+
65+
- `np.random.seed(15)` ensures reproducible results.
66+
- `np.random.randint(10)` generates a single integer between 0 and 9.
67+
- `np.random.randint(1, 6, size=5)` generates an array of 5 integers between 1 and 5.
68+
69+
## Codebyte Example
70+
71+
This codebyte sample generates a single random integer and a 2×3 array of random integers from specified ranges, ensuring reproducible results using a fixed random seed:
72+
73+
```codebyte/python
74+
import numpy as np
75+
np.random.seed(42)
76+
77+
# Single random integer between 0 and 9
78+
num = np.random.randint(10)
79+
print(f"Random integer: {num}")
80+
81+
# 2x3 array of random integers between 1 and 10
82+
arr = np.random.randint(1, 11, size=(2, 3))
83+
print(f"Random integers array:\n{arr}")
84+
```
85+
86+
- `np.random.seed(42)` ensures the code produces the same results every time.
87+
- `size=(2, 3)` generates a 2D array with 2 rows and 3 columns of random integers.

0 commit comments

Comments
 (0)