Issues with std::shuffle compiling

Mar 29, 2017 at 8:03pm
I'm unable to compile my code with std::shuffle accepting my random number as the third argument. I get the following error: Type 'int' cannot be used prior to '::' because it has no members algorithm
1
2
3
4
5
6
7
8
9
10
//random number
    int randNum;
    
    //seed randomNum generator
    srand(time(NULL));
    randNum = rand();
    
    //Shuffle Deck of Cards, use std::shuffle with rand num generator
    //std::random_shuffle(deckOfCards.begin(), deckOfCards.end());
    std::shuffle(deckOfCards.begin(), deckOfCards.end(), randNum);
Mar 29, 2017 at 9:05pm
std::shuffle is meant to be used with the random number engines from the <random> header.
http://en.cppreference.com/w/cpp/numeric/random

1
2
3
4
5
// Create and seed the random engine.
std::default_random_engine randomEngine(std::time(nullptr));

// Shuffle the deck of cards.
std::shuffle(deckOfCards.begin(), deckOfCards.end(), randomEngine);
Last edited on Mar 29, 2017 at 9:11pm
Mar 29, 2017 at 9:07pm
You need to supply a uniform random number generator as third argument not an int.
http://www.cplusplus.com/reference/algorithm/shuffle/
Topic archived. No new replies allowed.