Random Integers and Sampling
Learn how to generate random integers, select random elements from NumPy arrays, perform sampling with and without replacement, apply weighted probabilities, shuffle datasets, and generate random permutations. Explore practical examples, detailed outputs, common mistakes, and student selection applications using NumPy.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Generate random integers using
rng.integers(). - Understand lower and upper boundaries in integer generation.
- Create random integer arrays of different shapes.
- Select elements from arrays using
rng.choice(). - Understand sampling with and without replacement.
- Perform weighted sampling using probability values.
- Generate random permutations using
rng.permutation(). - Shuffle arrays using
rng.shuffle(). - Understand random seeds and reproducible results.
- Apply random sampling techniques to practical datasets.
2. Introduction to Random Integers and Sampling
Random integer generation is the process of producing whole numbers from a specified range. Random sampling is the process of selecting elements from a population or dataset according to a particular selection rule.
NumPy provides random number generation functionality through
the numpy.random module. For new Python programs,
the recommended approach is to create a generator using
np.random.default_rng().
import numpy as np
rng = np.random.default_rng()
numbers = rng.integers(1, 11, size=5)
print(numbers)
Example output:
[3 8 1 10 5]
Five random integers are generated between 1 and 10, including
both 1 and 10 because the upper boundary supplied to
integers() is 11.
The displayed output is illustrative. Actual values depend on the generator state and seed.
Where Is Random Sampling Used?
- Selecting students for a practical demonstration.
- Creating sample marks for educational exercises.
- Randomizing records before machine learning training.
- Generating test data for software applications.
- Performing probability experiments and simulations.
- Selecting random records from a large numerical dataset.
3. Generating Random Integers Using integers()
The integers() method generates random integer
values. It supports generating a single value, a one-dimensional
array, or a multidimensional array.
rng.integers(
low,
high=None,
size=None,
dtype=np.int64,
endpoint=False
)
Important Parameters
| Parameter | Description |
|---|---|
low |
The lower boundary. When high is specified, low is included in the range. |
high |
The upper boundary. It is excluded by default. |
size |
Specifies the number and shape of generated values. |
dtype |
Specifies the integer data type of the generated values. |
endpoint |
When True, the upper boundary is included. |
Example 1: Generate a Single Random Integer
import numpy as np
rng = np.random.default_rng(10)
number = rng.integers(1, 101)
print(number)
The generated value is an integer from 1 through 100. The upper boundary 101 is excluded.
Example 2: Generate Random Integers Between 1 and 10
import numpy as np
rng = np.random.default_rng(10)
numbers = rng.integers(1, 11, size=6)
print(numbers)
print("Number of elements:", numbers.size)
Example output:
Number of elements: 6The array contains six integers. Every generated value is at least 1 and less than 11.
Example 3: Include the Upper Boundary
import numpy as np
rng = np.random.default_rng(10)
numbers = rng.integers(
1,
10,
size=5,
endpoint=True
)
print(numbers)
Here, the generated values can include 10 because
endpoint=True makes the upper boundary inclusive.
rng.integers(1, 10) generates values from 1 through 9.
To include 10, use rng.integers(1, 10, endpoint=True)
or use rng.integers(1, 11).
4. Creating One-Dimensional and Multidimensional Random Arrays
The size parameter determines the shape of the
generated array. An integer creates a one-dimensional array
when greater than one, while a tuple specifies a multidimensional
shape.
Example 1: One-Dimensional Array
import numpy as np
rng = np.random.default_rng(20)
numbers = rng.integers(1, 50, size=5)
print(numbers)
print("Shape:", numbers.shape)
print("Dimensions:", numbers.ndim)
Output shape:
Shape: (5,)
Dimensions: 1
The array contains five random integers and has one dimension.
Example 2: Two-Dimensional Array
import numpy as np
rng = np.random.default_rng(20)
matrix = rng.integers(1, 100, size=(3, 4))
print(matrix)
print("Shape:", matrix.shape)
The generated matrix has three rows and four columns. Each value is an integer between 1 and 99.
Example 3: Three-Dimensional Array
import numpy as np
rng = np.random.default_rng(20)
data = rng.integers(0, 10, size=(2, 3, 4))
print(data)
print("Shape:", data.shape)
print("Dimensions:", data.ndim)
Output shape:
Shape: (2, 3, 4)
Dimensions: 3
This array contains two blocks, with three rows and four columns in each block.
5. Choosing Random Elements Using choice()
The choice() method selects random elements from
a supplied sequence or array. It can also select indices from
an integer population.
rng.choice(
a,
size=None,
replace=True,
p=None
)
Important Parameters
| Parameter | Description |
|---|---|
a |
An array or sequence to sample from, or an integer representing a population of indices. |
size |
The number or shape of selected elements. |
replace |
Determines whether an element can be selected multiple times in one sampling operation. |
p |
Optional probabilities associated with population elements. |
Example 1: Select One Random Fruit
import numpy as np
rng = np.random.default_rng(30)
fruits = np.array([
"Apple",
"Mango",
"Banana",
"Orange"
])
selected_fruit = rng.choice(fruits)
print(selected_fruit)
The program selects one element from the fruit array. The selected fruit depends on the generator state.
Example 2: Select Three Random Elements
import numpy as np
rng = np.random.default_rng(30)
fruits = np.array([
"Apple",
"Mango",
"Banana",
"Orange"
])
selected = rng.choice(fruits, size=3)
print(selected)
By default, replace=True, so repeated fruits
are possible.
Example 3: Select Random Integer Indices
import numpy as np
rng = np.random.default_rng(30)
indices = rng.choice(10, size=4)
print(indices)
This selects four indices from 0 through 9. Repeated indices are possible because replacement is enabled.
6. Sampling With Replacement
Sampling with replacement means that after an element is selected, it remains available for subsequent selections in the same sampling operation. Therefore, the same element can appear multiple times.
import numpy as np
rng = np.random.default_rng(40)
numbers = np.array([10, 20, 30, 40, 50])
sample = rng.choice(
numbers,
size=8,
replace=True
)
print(sample)
Eight elements are selected from a population containing five values. Since replacement is enabled, duplicate values are allowed.
Example: Repeated Random Marks
import numpy as np
rng = np.random.default_rng(40)
marks = np.array([60, 70, 80, 90])
sampled_marks = rng.choice(
marks,
size=6,
replace=True
)
print(sampled_marks)
This is useful when performing repeated random trials or when a sample may contain the same population value more than once.
7. Sampling Without Replacement
Sampling without replacement means that a selected element cannot be selected again during the same sampling operation. Every selected element is unique with respect to its population position.
import numpy as np
rng = np.random.default_rng(40)
numbers = np.array([10, 20, 30, 40, 50])
sample = rng.choice(
numbers,
size=3,
replace=False
)
print(sample)
Three different elements are selected from the five-element array. The order of selected elements is random.
Example: Select Two Students
import numpy as np
rng = np.random.default_rng(45)
students = np.array([
"Samadhan",
"Pradnya",
"Samiksha"
])
selected_students = rng.choice(
students,
size=2,
replace=False
)
print(selected_students)
Two different students are selected. A student cannot be selected twice in this operation.
What Happens If the Sample Is Too Large?
import numpy as np
rng = np.random.default_rng(40)
numbers = np.array([10, 20, 30])
sample = rng.choice(
numbers,
size=4,
replace=False
)
This raises a ValueError because four distinct
population elements cannot be selected from an array containing
only three elements.
| Feature | With Replacement | Without Replacement |
|---|---|---|
| Argument | replace=True |
replace=False |
| Repeated selections | Allowed | Not allowed |
| Sample larger than population | Allowed | Not allowed |
| Common use | Repeated random trials | Unique participant selection |
8. Weighted Random Sampling Using Probabilities
In weighted sampling, different population elements have different
probabilities of being selected. NumPy supports weighted selection
through the p parameter of choice().
The probability array must contain one probability per population element. Its values must be valid probabilities and sum to 1, allowing for floating-point tolerance.
Example 1: Weighted Category Selection
import numpy as np
rng = np.random.default_rng(50)
categories = np.array([
"Low",
"Medium",
"High"
])
probabilities = [0.20, 0.50, 0.30]
selected = rng.choice(
categories,
size=10,
p=probabilities
)
print(selected)
The configured probabilities are:
- Low: 20%
- Medium: 50%
- High: 30%
These probabilities influence the selection process, but a sample of ten elements is not guaranteed to contain exactly two Low, five Medium, and three High values.
Example 2: Weighted Student Selection
import numpy as np
rng = np.random.default_rng(50)
students = np.array([
"Samadhan",
"Pradnya",
"Samiksha"
])
probabilities = [0.50, 0.30, 0.20]
selected = rng.choice(
students,
size=2,
replace=False,
p=probabilities
)
print("Selected students:", selected)
The probabilities provide different selection weights.
Since replace=False, two different students
are selected.
Verify the Probability Sum
import numpy as np
probabilities = np.array([0.20, 0.50, 0.30])
print("Probability sum:", probabilities.sum())
Output:
Probability sum: 1.0choice().
9. Generating Random Permutations
A permutation is an arrangement of elements in a particular order.
The permutation() method returns a permuted result
without changing the original input array.
Example 1: Permutation of Integers
import numpy as np
rng = np.random.default_rng(60)
result = rng.permutation(10)
print(result)
The result contains each integer from 0 through 9 exactly once, arranged in a random order.
Example 2: Permute an Existing Array
import numpy as np
rng = np.random.default_rng(60)
marks = np.array([75, 82, 91, 68, 88])
shuffled_marks = rng.permutation(marks)
print("Original:", marks)
print("Permuted:", shuffled_marks)
The original array remains unchanged. The returned array contains the same values in a different order.
Example 3: Permute Rows of a Matrix
import numpy as np
rng = np.random.default_rng(60)
student_data = np.array([
["Samadhan", "78"],
["Pradnya", "92"],
["Samiksha", "85"]
])
shuffled_data = rng.permutation(
student_data,
axis=0
)
print(shuffled_data)
print("Original data:")
print(student_data)
The rows are permuted together, so each student's name stays associated with the correct mark. The original matrix is unchanged.
rng.permutation() when you need a shuffled result
but want to preserve the original array.
10. Shuffling Arrays Using shuffle()
The shuffle() method randomly changes the order
of elements in an array in place. This means the original array
is modified directly.
Example 1: Shuffle a One-Dimensional Array
import numpy as np
rng = np.random.default_rng(70)
numbers = np.array([10, 20, 30, 40, 50])
rng.shuffle(numbers)
print(numbers)
The array elements are rearranged. The values remain the same, but their positions can change.
Example 2: Shuffle Rows of a Two-Dimensional Array
import numpy as np
rng = np.random.default_rng(70)
data = np.array([
[1, 100],
[2, 200],
[3, 300],
[4, 400]
])
rng.shuffle(data, axis=0)
print(data)
The rows are shuffled along axis 0. Values belonging to the same row stay together.
Example 3: Understand the Return Value
import numpy as np
rng = np.random.default_rng(70)
numbers = np.array([1, 2, 3, 4, 5])
result = rng.shuffle(numbers)
print("Shuffled array:", numbers)
print("Return value:", result)
Output for the return value:
Return value: None
The method changes the array in place and returns
None.
numbers = rng.shuffle(numbers).
That assignment replaces the variable with None.
Call rng.shuffle(numbers) directly.
11. Difference Between choice(), permutation(), and shuffle()
These three methods are useful for different random selection and reordering tasks.
| Method | Purpose | Original Array | Return Value |
|---|---|---|---|
choice() |
Select random elements | Not modified by the selection | Selected value or array |
permutation() |
Return a random ordering | Unchanged | Permuted result |
shuffle() |
Reorder an array in place | Modified | None |
Quick Comparison Example
import numpy as np
rng = np.random.default_rng(80)
original = np.array([10, 20, 30, 40, 50])
sample = rng.choice(original, size=3, replace=False)
permuted = rng.permutation(original)
rng.shuffle(original)
print("Sample:", sample)
print("Permuted copy:", permuted)
print("Shuffled original:", original)
The sample contains selected elements, the permutation returns a reordered result, and the shuffle changes the original array.
12. Reproducibility Using Random Seeds
A random seed initializes a random number generator. Using the same seed and the same sequence of operations allows the same pseudo-random results to be reproduced in the same compatible environment.
Example: Reproducible Integer Arrays
import numpy as np
rng1 = np.random.default_rng(100)
sample1 = rng1.integers(1, 101, size=5)
rng2 = np.random.default_rng(100)
sample2 = rng2.integers(1, 101, size=5)
print("First sample:", sample1)
print("Second sample:", sample2)
print("Are equal:", np.array_equal(sample1, sample2))
Output for the comparison:
Are equal: TrueBoth generators use the same seed and perform the same operation. Therefore, the generated arrays match.
Why Are Seeds Useful?
- Reproducing classroom demonstrations.
- Testing random data generation in applications.
- Debugging programs that depend on random values.
- Comparing algorithms using repeatable test data.
- Reproducing experiments and simulations.
np.random.default_rng(seed)
when repeatable results are required. A seed does not make
the generated sequence truly random or cryptographically secure.
13. Practical Project: Random Student Selection
Project Description
CIIT TRANING INSTITUTE wants to select two students for a NumPy practical demonstration. The selection should be random, and no student should be selected more than once.
The available students are Samadhan, Pradnya, and Samiksha.
We will use choice() with
replace=False to select two different students.
Complete Program
import numpy as np
rng = np.random.default_rng(123)
students = np.array([
"Samadhan",
"Pradnya",
"Samiksha"
])
selected_students = rng.choice(
students,
size=2,
replace=False
)
print("Institute: CIIT TRANING INSTITUTE")
print("Selected students:")
for student in selected_students:
print(student)
The program selects two different students. The selected names depend on the generator's state and seed.
Practical Extension: Randomize Student Records
Suppose we have student names and their marks. We want to change the order of records without separating a name from its mark.
import numpy as np
rng = np.random.default_rng(123)
student_data = np.array([
["Samadhan", "78"],
["Pradnya", "92"],
["Samiksha", "85"]
])
randomized_data = rng.permutation(
student_data,
axis=0
)
print("Original records:")
print(student_data)
print("Randomized records:")
print(randomized_data)
Each complete row is moved as a unit. This is important when working with datasets containing multiple related columns.
14. Common Errors and Best Practices
Error 1: Incorrect Upper Boundary
import numpy as np
rng = np.random.default_rng(1)
numbers = rng.integers(1, 6, size=10)
print(numbers)
The generated values range from 1 through 5. The upper boundary
6 is excluded because endpoint=False is the default.
Error 2: Requesting Too Many Unique Elements
numbers = np.array([10, 20, 30])
# This raises ValueError:
sample = rng.choice(
numbers,
size=4,
replace=False
)
The sample size must not exceed the population size when sampling without replacement.
Error 3: Incorrect Probability Sum
import numpy as np
rng = np.random.default_rng(1)
values = np.array(["A", "B", "C"])
# Invalid: probabilities sum to 0.8
# rng.choice(values, size=5, p=[0.2, 0.3, 0.3])
# Correct: probabilities sum to 1.0
result = rng.choice(
values,
size=5,
p=[0.2, 0.3, 0.5]
)
print(result)
Error 4: Assigning the Return Value of shuffle()
# Incorrect:
# numbers = rng.shuffle(numbers)
# Correct:
rng.shuffle(numbers)
The shuffle method modifies the array directly and returns
None.
Best Practices
- Use
np.random.default_rng()for new code. - Check whether the upper boundary should be included.
- Choose replacement behavior according to the task.
- Ensure probabilities are valid and correspond to the population.
- Use
permutation()to preserve the original ordering. - Use
shuffle()when in-place modification is intended. - Set a seed when reproducible examples are required.
15. Practice Exercises
Complete the following exercises to strengthen your understanding of NumPy random integer generation and sampling.
- Generate ten random integers between 1 and 100, including both boundaries.
- Create a random integer array with four rows and three columns.
-
Select three elements from a fruit array using
choice(). - Select two different students from Samadhan, Pradnya, and Samiksha.
- Demonstrate sampling with replacement using an array of five numbers.
-
Perform weighted sampling using probabilities
[0.2, 0.3, 0.5]. - Generate a permutation of integers from 0 through 14.
- Shuffle an array in place and inspect its return value.
- Create two generators with the same seed and compare their generated integer arrays.
- Randomize student records while preserving each student's name and mark in the same row.
Summary
NumPy provides several tools for random integer generation
and sampling. The integers() method generates
whole numbers within a specified range, and its upper
boundary is excluded by default.
The choice() method selects elements from a
population. Use replace=True for sampling with
replacement and replace=False for sampling
without replacement. The p parameter supports
weighted sampling.
The permutation() method returns a permuted
result without changing the original input array.
The shuffle() method changes the array in place
and returns None.
A fixed seed supports reproducibility when the generator and sequence of operations remain the same. These techniques are useful for simulations, educational datasets, testing, and data preparation.