mardi 11 octobre 2022

c++ - deterministic alternative to std::uniform_XXX_distribution with mt19937?

I need a way to get deterministic sequences of ints and doubles.

template <class U>
constexpr auto get_random_value (std::mt19937 &gen, U min_value, U max_value)->U
{
    if constexpr ( std::is_same_v <U, double> or std::is_same_v <U, float> ){
        std::uniform_real_distribution <U> distrib( min_value, max_value );
 
        return distrib( gen );
    }
    else if constexpr ( std::is_same_v <U, u32> or std::is_same_v <U, i32> ){
        std::uniform_int_distribution distrib( min_value, max_value );

        return distrib( gen );
    }
    else {
        throw std::runtime_error( "error value type" );
    }
}

My issue is that one day to another, the same seeded value will lead to different results. The distribution is to blame because it goes a long way to avoid the pitfall of the modulo.

But I need a precise way to always be certain that a sequence will always be the same starting from a given seed. And I need an unbiased partition (so % and rand() are out).

What kind of implementation will guarantee this?




Aucun commentaire:

Enregistrer un commentaire