Skip to content

Instantly share code, notes, and snippets.

@nixiz
Last active June 18, 2019 07:42
Show Gist options
  • Save nixiz/dc75b49e3ab7d247179fddfac814c37b to your computer and use it in GitHub Desktop.
Save nixiz/dc75b49e3ab7d247179fddfac814c37b to your computer and use it in GitHub Desktop.
mersenne twister algorithm seed finder for desired sequence of string.
// MersenneTwisterSeedFinder.cpp
// Copyright (c) Oguzhan KATLI. All rights reserved.
#include <random>
#include <string>
#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <atomic>
#include <condition_variable>
using namespace std;
std::atomic_bool has_found(false);
template <typename integral_type = uint32_t>
integral_type find_mersenne_seed(const string& input)
{
struct MersenneSeedFinderBlock
{
std::string target_string;
integral_type seed_start;
integral_type seed_end;
integral_type seed_current;
const size_t str_size;
bool found;
MersenneSeedFinderBlock(
const std::string& _target_string,
integral_type _seed_start,
integral_type _seed_end = std::numeric_limits<integral_type>::max()) :
target_string(_target_string), seed_start(_seed_start), seed_end(_seed_end), str_size(_target_string.size()), found(false)
{ }
bool operator()()
{
std::string result;
result.reserve(str_size);
for (seed_current = seed_start; (seed_current <= seed_end) && (has_found == false); seed_current++)
{
result.clear();
std::mt19937_64 eng(seed_current);
std::generate_n(std::back_inserter(result), str_size, [&] {return static_cast<char>(eng() % 26 + 'a'); });
if (result == target_string)
{
found = true;
has_found = true;
return true;
}
}
return false;
}
integral_type seed() const
{
return seed_current;
}
};
has_found = false;
const auto thread_count = std::thread::hardware_concurrency();
const integral_type max_seed = std::numeric_limits<integral_type>::max();
std::vector<std::thread*> threadpool;
threadpool.reserve(thread_count);
const integral_type chunk_size = max_seed / thread_count;
integral_type chunk_start = 0;
integral_type chunk_end = 0;
integral_type result = 0;
//std::cout
// << "number of threads: " << thread_count << "\n"
// << "total iteration for each thread: " << chunk_size << "\n";
for (size_t i = 0; i < thread_count; i++)
{
chunk_end = chunk_size + (chunk_size * i);
threadpool.push_back(new std::thread([&] {
MersenneSeedFinderBlock m(input, chunk_start, chunk_end);
if (m())
result = m.seed();
}));
chunk_start = chunk_end;
}
for (std::thread* t : threadpool)
{
t->join();
delete t;
}
return result;
}
/*
seed numbers for given strings:
oguzhan : 1537228673098052682
okatli : 15510378 and 3940126393
*/
int main()
{
std::string input;
cout << "Enter string to find seed: ";
std::getline(cin, input);
cout << "finding seed number for given string: " << input << "\n";
auto result = find_mersenne_seed(input);
cout << "seed number: " << result << "\n";
return 0;
}
@nixiz
Copy link
Author

nixiz commented Jun 17, 2019

send threads std::promise and wait for future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment