1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <random>
#include <type_traits>
#include <algorithm>
template<typename T, size_t N>
void print(T(&a)[N]) {
for (size_t i = 0; i < N; ++i) std::cout << a[i] << ' ';
std::cout << '\n';
}
int main() {
using std::cout;
std::random_device rd;
std::seed_seq seeder{rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(),
rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()};
auto rnd{std::mt19937(seeder)};
rnd.discard(rd() % 256);
int a[] { 5, 10, 15, 20, 25, 30 };
int size = std::extent_v<decltype(a)>; // pre-C++17: extent<...>::value
std::uniform_int_distribution<> dist(0, size - 1);
// Choosing randomly from the entire array each time results
// in repeating values.
for (int i = 0; i < 20; ++i) cout << a[dist(rnd)] << ' ';
cout << '\n';
// shuffling gives you the numbers in a random order without repeats
for (int i = 0; i < 5; ++i) {
std::shuffle(a, a + size, rnd);
print(a);
}
}
| |