-2

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(rd());

    //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";
Ritchie Shatter
  • 185
  • 1
  • 4
  • 14
  • 1
    Downvoted for lack of research effort. One can hardly read any documentation on smth. like `mt19937` without learning what's wrong here, and searching for *"Seed mt19937 C++"* gives decent results with every search engine too. – Baum mit Augen May 04 '16 at 23:57
  • I was seeding mt19937 with different integers to see if anything would change. I accidentally posted that code here. I have fixed the code by seeding mt19937 with rd which is the intended implementation. std::random_device is a random seed is it not? – Ritchie Shatter May 05 '16 at 00:16
  • This doesn't even compile. And no, `std::random_device` is not a seed, although it can be used to get one. Please, go read some [docs](http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine). – Baum mit Augen May 05 '16 at 00:20
  • I do apologize for leaving out the opening and closing parentheses after rd. It should compile now. I must also admit I have looked at that very link but was still left a bit confused, so I thought I'd ask here since people usually are very helpful. Usually. – Ritchie Shatter May 05 '16 at 00:36
  • 1
    What's your system? MinGW's `random_device` is known to be broken. – T.C. May 05 '16 at 00:44
  • I'm on windows using Qt creator with MinGW. – Ritchie Shatter May 05 '16 at 00:54

2 Answers2

1

You're using the same, simple seed. Read up on how to properly seed the PRNG: How to properly seed a mersenne twister RNG?

Community
  • 1
  • 1
mrthotep
  • 21
  • 3
0

I decided to go with the following solution as it fulfills my general requirements.

vector<string> vect;

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

    srand(std::time(0));
    random_shuffle(vect.begin(), vect.end());

    vector<string>::const_iterator ivect = vect.begin();
    while (ivect != vect.end()) {
        deck.push(*ivect);
        ivect++;
    }

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

Here I am using std::random_suffleand setting the seed with srand(std::time(0));. This allows for a unique sequence each time.

Ritchie Shatter
  • 185
  • 1
  • 4
  • 14