I need to generate random variables for different bit counts. I came up with the following way to do it:
constexpr const std::size_t bit_count = 77U;
std::uint_fast32_t seed = 0U;
try
{
std::random_device rd;
seed = rd(); // may throw
}
catch(std::exception& e)
{
//simply use time as seed then
seed = std::chrono::system_clock::now().time_since_epoch().count();
}
std::mt19937 mt(seed);
using number_t = std::vector<bool>;
std::vector<number_t> numbers;
for(std::size_t h = 0U; i < how_many_numbers; ++h)
{
std::uniform_int_distribution<int> dist(0, 1);
numbers.push_back(number_t());
auto& num = numbers.back();
num.resize(bit_count);
for(std::size_t i = 0U; i < bit_count; ++i)
num[i] = (dist(mt) == 0);
}
Will this be uniformly distributed and behave almost as it would have been generated directly?
If not: How could you do so? I really need those different bit sizes for hashing purposes, so just using some primitive type is not that type of an option, especially since the bit count might be 8192, but not greater. Since the bit count is variable and for learning purposes, I want/need to do it by hand.
Aucun commentaire:
Enregistrer un commentaire