Created
March 28, 2019 05:42
-
-
Save comzyh/e8c4cc801dcf1066aa53267a765fb34c to your computer and use it in GitHub Desktop.
partial_random_shuffle.cpp
This file contains 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 <algorithm> | |
#include <random> | |
#include <iostream> | |
using namespace std; | |
template< class RandomIt> | |
void partial_random_shuffle( RandomIt first, RandomIt last, RandomIt part) { | |
typename std::iterator_traits<RandomIt>::difference_type i, n, p; | |
p = part - first; | |
n = last - first; | |
std::uniform_int_distribution<> dist(0, n - 1); | |
std::random_device rd; | |
std::default_random_engine rng {rd()}; | |
for (i = 0; i < p; i ++ ) { | |
std::swap(first[i], first[dist(rng)]); | |
} | |
} | |
int main() { | |
std::vector<int> v(100000); | |
for (size_t i = 0; i < v.size(); i++) { | |
v[i] = int(i); | |
} | |
partial_random_shuffle(v.begin(), v.end(), v.begin() + 100); | |
for (int i = 0; i < 100; i++) { | |
cout << v[i] << endl; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment