Skip to content

Instantly share code, notes, and snippets.

@odanado
Last active August 29, 2015 14:02
Show Gist options
  • Save odanado/e3c290b4e941e9cbe7d3 to your computer and use it in GitHub Desktop.
Save odanado/e3c290b4e941e9cbe7d3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <cstdint>
#include <cstdlib>
std::mutex print_mutex;
template <class T>
void print_hex(T x) {
std::lock_guard<std::mutex>lk(print_mutex);
std::cout<<std::hex<<x<<std::dec<<"\n";
}
namespace pokemon{
namespace rng {
class mt {
uint32_t mt_table[624];
uint32_t mt_count;
uint32_t first_seed;
uint32_t last_seed;
uint32_t ivs[6];
public:
mt(){}
mt(uint32_t f, uint32_t l, uint32_t array[6]) :
first_seed(f), last_seed(l) {
for(int i = 0; i < 6; i++) {
ivs[i] = array[i];
}
}
void operator() (){
for(uint32_t seed = first_seed; seed <= last_seed; seed++) {
init_mt(seed);
bool is_wrong = false;
for(int i = 0; i < 6; i++) {
uint32_t iv = genrand_int32() >> 27;
if(iv != ivs[i]) {
is_wrong = true;
break;
}
}
if(!is_wrong) {
print_hex(seed);
}
}
}
void init_mt(uint32_t s) {
mt_table[0] = s;
for(int i=1; i<624; i++) {
mt_table[i] = ((mt_table[i-1] >> 30) ^ mt_table[i-1]) * 0x6c078965 + i;
}
mt_count = 0;
}
uint32_t genrand_int32() {
uint32_t k;
uint32_t a,b,c;
a = mt_count;
b = mt_count == 623UL ? 0UL : mt_count + 1UL;
c = mt_count < 227UL ? mt_count + 397UL : mt_count - 227UL;
k = (mt_table[a] & 0x80000000UL) | (mt_table[b] & 0x7FFFFFFFUL);
k = (k >> 1) ^ mt_table[c];
if(k&1) {
mt_table[a] = k ^ 0x9908B0DFUL;
}
else {
mt_table[a] = k;
}
k = mt_table[a];
k ^= (k >> 11);
k ^= (k << 7) & 0x9D2C5680UL;
k ^= (k << 15) & 0xEFC60000UL;
k ^= (k >> 18);
mt_count = b;
return k;
}
};
} // namespace rng
} // namespace pokemon
int main(int argc, char *argv[]) {
uint32_t ivs[6] = {};
if(argc!=8) {
std::cout<<"usage: [Number of threads] [ivs]\n";
return 0;
}
uint32_t n = atoi(argv[1]);
uint32_t seg = 0xFFFFFFFFUL / n;
pokemon::rng::mt* mt = new pokemon::rng::mt[n];
std::vector<std::thread> ths;
ths.reserve(n);
for(int i = 2; i < 8; i++) {
ivs[i-2] = atoi(argv[i]);
}
for(uint32_t i = 0; i < n; i++) {
mt[i] = pokemon::rng::mt(seg*i ,seg*(i+1),ivs);
ths[i] = std::thread(mt[i]);
}
for(uint32_t i = 0; i < n; i++) {
ths[i].join();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment