Last active
December 22, 2022 02:50
-
-
Save boydjc/89704a83308ed3d60aaacd32d83bac05 to your computer and use it in GitHub Desktop.
Random Sample Without Replacement C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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