8 Key Standard Library Algorithms

# Ensuring No Repeated Elements in std::ranges::sample()

## Can I use std::ranges::sample() to ensure no repeated elements in the sample?

Yes, std::ranges::sample() ensures no repeated elements in the sample by itsÂ design.

The function samples without replacement, meaning once an element is selected, it cannot be selected again within the same sampleÂ operation.

Here is an example demonstrating thisÂ behavior:

#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
#include <ranges>

int main() {
std::vector<int> numbers{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> output;
output.resize(5);

auto rng = std::mt19937{std::random_device{}()};

std::ranges::sample(
numbers, output.begin(), output.size(), rng);

std::ranges::for_each(output, [](int n) {
std::cout << n << ", ";
});
}
4, 9, 2, 7, 1,

In this example, std::ranges::sample() randomly selects 5 elements from the numbers vector without replacement. This ensures that each element in the output vector isÂ unique.

If you need to perform sampling multiple times and want to ensure that the results are unique across multiple samples, you will need to adjust the source vector or handle itÂ programmatically.

For example, to draw multiple unique samples from the same sourceÂ vector:

#include <algorithm>
#include <iostream>
#include <random>
#include <ranges>
#include <vector>

int main() {
std::vector<int> numbers{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> output1(5);
std::vector<int> output2(5);

auto rng = std::mt19937{std::random_device{}()};

// First sample
std::ranges::sample(
numbers, output1.begin(), output1.size(), rng);

std::ranges::for_each(output1, [](int n) {
std::cout << n << ", ";
});
std::cout << "\n";

// Remove sampled elements from the source
for (int n : output1) {
numbers.erase(
std::remove(numbers.begin(), numbers.end(), n),
numbers.end()
);
}

// Second sample from the remaining elements
std::ranges::sample(
numbers, output2.begin(), output2.size(), rng);
std::ranges::for_each(output2, [](int n) {
std::cout << n << ", ";
});
}
4, 9, 2, 7, 1,
8, 3, 10, 5, 6,

In this example, the first sample is taken, and then those elements are removed from the original numbers vector before taking the second sample. This ensures that no element is repeated across the twoÂ samples.

By using std::ranges::sample() and managing the source vector, you can ensure unique elements in your samplesÂ effectively.

This Question is from the Lesson:

### 8 Key Standard Library Algorithms

An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques

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

This Question is from the Lesson:

### 8 Key Standard Library Algorithms

An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques

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.