Movement Algorithms

# Shuffling a Partial Range

## Can you use std::ranges::shuffle() with a partial range of a container, and how?

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.

### Using std::ranges::shuffle() with a Partial Range

To 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

### Step-by-Step Guide

1. Define the Range: Identify the portion of the container you want to shuffle. Use iterators to specify the start and end of this subrange.
2. Provide a Random Number Generator (RNG): std::ranges::shuffle() requires an RNG. Here, we use std::mt19937 from the <random> header.
3. Call the std::ranges::shuffle algorithm: Pass the iterators for the subrange and the RNG to std::ranges::shuffle().

### Practical Example

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

### Considerations

• Range Boundaries: Ensure that the iterators provided do not exceed the containerâ€™s boundaries to avoid undefined behavior.
• Random Device: Using std::random_device for seeding the RNG ensures better randomness compared to a fixed seed.

### Conclusion

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.

This Question is from the Lesson:

### Movement Algorithms

An introduction to the seven movement algorithms in the C++ standard library: move(), move_backward(), rotate(), reverse(), shuffle(), shift_left(), and shift_right().

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

### Movement Algorithms

An introduction to the seven movement algorithms in the C++ standard library: move(), move_backward(), rotate(), reverse(), shuffle(), shift_left(), and shift_right().

Part of the course:

## Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

### This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

### Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.