vendredi 5 juin 2015

Default_random_engine passed into a function gives repeatable results

I have a class Permutation that inherits from std::vector<int>. I created a constructor that makes the object filled with non-repeating numbers. Randomness is meant to be guaranteed by <random> stuff, so the declaration goes like this:

/* Creates a random permutation of a given length
 * Input: n - length of permutation
 *        generator - engine that does the randomizing work */
Permutation(int n, default_random_engine generator);

Function itself looks like this (irrevelant details skipped):

Permutation::Permutation(int n, default_random_engine generator):
vector<int>(n, 0)
{
    // we build a path-represented permutation first, and then convert it
    vector<int> someIntermediateStep(n, 0);
    iota(someIntermediateStep.begin(), someIntermediateStep.end(), 0); //0, 1, 2...

    shuffle(someIntermediateStep.begin(), someIntermediateStep.end(), 
            generator);

    // etc.
 }

And is called in the following context:

auto seed = std::chrono::system_clock::now().time_since_epoch().count();
static std::default_random_engine generator(seed);

for (int i = 0; i < n; i++) 
    Permutation test(length, generator);

Code compiles perfectly fine, but all instances of Permutation are the same. How to force regular generation of random numbers? I know that default_random_engine should be binded to a distribution object, but hey, I don't have any – I use the engine only in shuffle() (at least at the moment).

Is there any solution or a workaround that still uses the goodness of <random>?

Aucun commentaire:

Enregistrer un commentaire