mercredi 4 mai 2016

Shuffle deck of cards using std::random_device

I am trying to shuffle the contents of a vector. After the vector is sorted, the contents will be transferred into a queue. The queue represents the deck of cards. The issue I am having is that I continuously receive the same results even if I close the program. In fact I get the same sequence even after closing the IDE. In other words the cards are shuffled once and stay in that order no matter what. I have tried using this new method of randomizing contents before and had to resort to the old method. I hope to finally resolve this issue once and for all. If there is no way to accomplish this, my next question would be how to randomize the contents while still using std::shuffle. I have also tried auto engine = std::default_random_engine{}; only to obtain the same results. Thanks.

    std::vector<string> v;

    map<string, int>::const_iterator iter;
    for (iter = cards.begin(); iter != cards.end(); iter++) {
       v.push_back(iter->first);
    }

    std::random_device rd;
    std::mt19937 g(3);

    //auto engine = std::default_random_engine{};
    std::shuffle(v.begin(), v.end(), g);

    std::copy(v.begin(), v.end(), std::ostream_iterator<string>(std::cout, " "));
    std::cout << "\n";




Aucun commentaire:

Enregistrer un commentaire