Yes, you can use std::ranges::shuffle()
with a partial range of a container. This is useful when you want to randomize only a specific portion of a container while leaving the rest intact.
std::ranges::shuffle()
with a Partial RangeTo shuffle a partial range, you can provide iterators that define the subrange you want to shuffle. Here’s an example:
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main() {
std::vector<int> data{1, 2, 3, 4, 5, 6, 7, 8};
std::random_device rd;
std::mt19937 gen(rd());
// Shuffle only the first 5 elements
std::ranges::shuffle(data.begin(),
data.begin() + 5, gen);
for (const auto& value : data) {
std::cout << value << " ";
}
}
4 1 3 5 2 6 7 8
std::ranges::shuffle()
requires an RNG. Here, we use std::mt19937
from the <random>
header.std::ranges::shuffle
algorithm: Pass the iterators for the subrange and the RNG to std::ranges::shuffle()
.Consider a scenario where you want to shuffle only the middle part of a vector:
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main() {
std::vector<int> numbers{
10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
std::random_device rd;
std::mt19937 gen(rd());
// Shuffle elements from index 3 to 7
std::ranges::shuffle(
numbers.begin() + 3,
numbers.begin() + 8,
gen
);
for (const auto& num : numbers) {
std::cout << num << " ";
}
}
10 20 30 80 70 60 40 50 90 100
std::random_device
for seeding the RNG ensures better randomness compared to a fixed seed.Using std::ranges::shuffle()
with a partial range of a container is straightforward.
By specifying the appropriate iterators, you can shuffle any subrange within your container, giving you flexibility in how you randomize elements.
This approach is particularly useful in scenarios where only a segment of data needs to be randomized.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to the seven movement algorithms in the C++ standard library: move()
, move_backward()
, rotate()
, reverse()
, shuffle()
, shift_left()
, and shift_right()
.