Make myrandom
a static member function of your Game
class.
random_shuffle not allowing me to pass it a seed
-
30-09-2022 - |
Question
so I have a vector of cards and I'm trying to shuffle it using the random_shuffle method, but it's not shuffling them by only passing it the first and last item. So I know there's a third parameter which is a custom seed, the thing it, random_shuffle says it doesn't find any functions overloaded with these parameters (Or something like that - my program's in spanish)
Basically, I have two vectors, enemyCards and playerCards, both have a size of 5.
random_shuffle(enemyCards.begin(), enemyCards.end());
random_shuffle(playerCards.begin(), playerCards.end());
This compiles but is not working, while
int myrandom (int i) { return std::rand()%i;}
random_shuffle(enemyCards.begin(), enemyCards.end(), myrandom);
random_shuffle(playerCards.begin(), playerCards.end(), myrandom);
doesn't even compile.
Am I doing something wrong?
I have a srand() at the beginning and other working rand() in the code, so the that should be working fine, it's just the random_shuffle that does not.
This is what the compiler says when using the myrandom method:
Error 11 error C3867: 'comgrid::Game::myrandom': function call missing argument list; use '&comgrid::Game::myrandom' to create a pointer to member
Error 12 error C2780: 'void _STL::random_shuffle(_RandomAccessIter,_RandomAccessIter)' : expects 2 arguments - 3 provided
Which is strange, because it's really 1:1 as the example on cplusplus.
Solution
OTHER TIPS
One does not pass a seed to random_shuffle
: the optional third argument is a function object that must return a random integer in [0,n) when passed n
as an argument. The following SSCCE demonstrates the use of random_shuffle
(and the "better" C++11 variant shuffle
) for your 5 element vectors (Live code at Coliru):
void dump_one(const char* name, vector<int> const& v) {
cout << name << ": {" << v[0];
for (auto i = size_t{1}; i < v.size(); ++i) {
cout << ',' << v[i];
}
cout << "}\n";
}
int myrandom(int i) { return rand() % i; }
int main() {
srand(random_device{}());
auto playerCards = vector<int>{1,2,3,4,5};
auto enemyCards = vector<int>{6,7,8,9,10};
// Variant 1: use std::rand
random_shuffle(begin(playerCards), end(playerCards));
random_shuffle(begin(enemyCards), end(enemyCards));
dump_one("player", playerCards);
dump_one("enemy", enemyCards);
// Variant 2: use functor
random_shuffle(begin(playerCards), end(playerCards), myrandom);
random_shuffle(begin(enemyCards), end(enemyCards), myrandom);
dump_one("player", playerCards);
dump_one("enemy", enemyCards);
// C++11: Use shuffle instead of random_shuffle
auto engine = mt19937_64{random_device{}()};
shuffle(begin(playerCards), end(playerCards), engine);
shuffle(begin(enemyCards), end(enemyCards), engine);
dump_one("player", playerCards);
dump_one("enemy", enemyCards);
}
The techniques that you say "don't work" are working correctly, so the problem with your program is not in the things you are describing to us.