-1

For fun im making a singleton that is used to shuffle an array. Im trying to shuffle an array in c++ with std::shuffle, but the shuffle produces the same result everytime I run the program.

The code:

// Example program
#include <iostream>
#include <string>
#pragma once
#include <memory>   
#include <random>
#include <ctime>
#include <algorithm>


class RandomPicker
{
public:
    ~RandomPicker();
    std::default_random_engine getRandomEngine();
    static std::shared_ptr<RandomPicker> getInstance();
private:
    std::default_random_engine dre = std::default_random_engine(time(0));
    RandomPicker();
    static std::shared_ptr<RandomPicker> instance;
};


std::shared_ptr<RandomPicker> RandomPicker::instance = nullptr;

RandomPicker::RandomPicker()
{
}


RandomPicker::~RandomPicker()
{
}

std::default_random_engine RandomPicker::getRandomEngine()
{
    return std::default_random_engine();
}

std::shared_ptr<RandomPicker> RandomPicker::getInstance()
{

    if (instance == nullptr)
    {
        instance.reset(new RandomPicker);
    }
    return instance;
}



int main()
{
   std::array<int,5> foo {1,2,3,4,5};

    std::shared_ptr<RandomPicker> r = RandomPicker::getInstance();
     shuffle (foo.begin(), foo.end(), r->getRandomEngine());


  std::cout << "shuffled elements:";
  for (int& x: foo) std::cout << ' ' << x;
  std::cout << '\n';


}

Link to code showing it:

I thought using

std::default_random_engine dre = std::default_random_engine(time(0));
//notice the time(0)

would get a different result everytime, but the result equals to.

shuffled elements: 3 1 5 4 2

Why is the array not sorted in a different way everytime the program runs?

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • Possible duplicate of: [std::default_random_engine generato values betwen 0.0 and 1.0](http://stackoverflow.com/questions/15461140/stddefault-random-engine-generato-values-betwen-0-0-and-1-0). *"I thought using [time(0)] would get a different result everytime, but the result equals to `shuffled elements: 3 1 5 4 2`"* - for me it produces the different result (as expected). Are you sure that you've recompiled the program? – awesoon Aug 19 '16 at 11:29
  • @soon yes i also get the result online – Sven van den Boogaart Aug 19 '16 at 11:33
  • I've changed your `getRandomEngine` and now it works as expected: [http://cpp.sh/5di4](http://cpp.sh/5di4) – awesoon Aug 19 '16 at 11:36
  • Since this question is simply about a bug in your code, I suggest you delete it. – einpoklum Aug 26 '16 at 08:19
  • @einpoklum I can't it got an answer. – Sven van den Boogaart Aug 26 '16 at 08:39

2 Answers2

8
std::default_random_engine RandomPicker::getRandomEngine()
{
    return std::default_random_engine();
}

You getRandomEngine() always returns an engine with default seed.

for_stack
  • 21,012
  • 4
  • 35
  • 48
1

Your getRandomEngine() should probably return dre:

std::default_random_engine RandomPicker::getRandomEngine()
{
    return dre;
}
Moberg
  • 5,253
  • 4
  • 38
  • 54