Skip to content

Instantly share code, notes, and snippets.

@boydjc
Last active December 22, 2022 02:50
Show Gist options
  • Save boydjc/89704a83308ed3d60aaacd32d83bac05 to your computer and use it in GitHub Desktop.
Save boydjc/89704a83308ed3d60aaacd32d83bac05 to your computer and use it in GitHub Desktop.
Random Sample Without Replacement C++
#include <numeric>
#include <random>
int randNumbersNeeded = 10;
int minRange = 1, maxRange = 10;
std::random_device rd;
std::mt19937 g(rd());
std::vector<int> intVector(maxTicker);
// initialize int vector and fill it to max range
std::iota(intVector.begin(), intVector.end(), minRange);
// shuffle the vector
std::shuffle(intVector.begin(), intVector.end(), g);
// take the nth elements from the beginning of the vector
std::vector<int> sampledNumbers(intVector.begin(), (intVector.end() - maxRange) + randNumbersNeeded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment