Skip to content

Instantly share code, notes, and snippets.

@tclamb
Created April 25, 2018 06:05
Show Gist options
  • Save tclamb/1ad6b84a8dad139fa07348346e277a44 to your computer and use it in GitHub Desktop.
Save tclamb/1ad6b84a8dad139fa07348346e277a44 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <mutex>
#include <random>
#include <thread>
using namespace std;
using u32 = uint_fast32_t;
using nl = numeric_limits<u32>;
mutex m;
auto find_seed(u32 max, u32 delta) {
return [=]{
thread_local mt19937 gen;
thread_local uniform_int_distribution<u32> dist{0, 3};
thread_local const vector<u32> v{{0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0, 3}};
for (auto i = max - delta; i < max; ++i) {
gen.seed(i);
if (all_of(begin(v), end(v), [&](auto x){ return x == dist(gen); })) {
unique_lock<mutex> l{m};
cout << i << '\n';
}
}
};
}
int main() {
vector<thread> v;
for (auto i = thread::hardware_concurrency(), d = nl::max() / i; i > 0; --i) {
v.emplace_back(find_seed(i * d, d));
}
for (auto& t : v) {
t.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment