dimanche 24 avril 2016

std::random_shuffle not being seeded

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>


int main() {
    std::vector<short> a(256);
    for (short x = 0; x != 256; ++x) {
        a[x] = x;
    }
    for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
    std::cout << std::endl;

    std::srand(11);

    std::random_shuffle(a.begin(), a.end());
    for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
    std::cout << std::endl;

    for (short x = 0; x != 256; ++x) {
        a[x] = x;
    }
    for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
    std::cout << std::endl;

    std::srand(11);

    std::random_shuffle(a.begin(), a.end());
    for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
}

So, here is my code. What I expect is, obviously, the same shuffle both times. What I get is, while the shuffles are consistent between launches, they are different and seem to ignore srand! What am I doing wrong here?




Aucun commentaire:

Enregistrer un commentaire