<random> will not work

I am using <algorithm> random_shuffle and realized that its gives me the same randomness every time I shuffle so I tried using <random> to fix this but I keep getting an error that I don't understand -

In file included from /usr/include/c++/4.8/algorithm:62:0,
from Deck.cpp:4:
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘void std::random_shuffle(_RAIter, _RAIter, _Generator&&) [with _RAIter = __gnu_cxx::__normal_iterator<Card**, std::vector<Card*> >; _Generator = std::linear_congruential_engine<unsigned int, 16807u, 0u, 2147483647u>]’:
Deck.cpp:32:70: required from here
/usr/include/c++/4.8/bits/stl_algo.h:5230:58: error: no match for call to ‘(std::linear_congruential_engine<unsigned int, 16807u, 0u, 2147483647u>) (__gnu_cxx::__normal_iterator<Card**, std::vector<Card*> >::difference_type)’
std::iter_swap(__i, __first + __rand((__i - __first) + 1));
^
In file included from /usr/include/c++/4.8/random:50:0,
from /usr/include/c++/4.8/bits/stl_algo.h:65,
from /usr/include/c++/4.8/algorithm:62,
from Deck.cpp:4:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1 #include <iostream>
  2 #include <ostream>
  3 #include <vector>
  4 #include <algorithm>
  5 #include <random>
  6 #include <chrono>
  7 #include "Deck.h"
  8 #include "Card.h"
  9 
 10 using namespace std;
 11 
 12 //Constuctor. Param determines how many sets of cards you want in a deck.
 13 Deck::Deck(int numSets){
 14 
 15 Card *card;
 16 //Create deck by putting cards into _deck vector.
 17 int finished = 0; 
 18 while(finished != numSets){
 19         for(int i=1; i<5; i++){
 20                 for(int j=2; j<15; j++){
 21                         card = new Card(j,i);
 22                         _deck.push_back(card);
 23                 }
 24         }
 25         finished++;
 26         }
 27 }
 28 
 29 //Shuffle deck.
 30 void Deck::shuffle(){ 
 31         unsigned seed = chrono::system_clock::now().time_since_epoch().count();
 32         random_shuffle(_deck.begin(),_deck.end(),default_random_engine(seed));
 33 }
 34 
 35 //Deal one card.
 36 void Deck::dealOne(){ 
 37         Card *topCard = _deck.front();
 38         topCard->printCard();
 39         //For Now the Card will be removed from. Later on this will be transferred
 40         // to the game class.
 41         _deck.erase(_deck.begin());
 42 }
 43 
 44 int Deck::_randN(){
 45         return rand();
 46 }
 47 
 48 //Prints every card in the deck.
 49 void Deck::printDeck(){
 50         int dkSize = _deck.size();
 51         for(int i=0; i<dkSize; i++){
 52         _deck[i]->printCard();
 53         }
 54 }
random_shuffle is deprecated, use shuffle instead.

random_shuffle takes not Random Bit Geterator, but function f which returns random number from 0 to n if invoked as f(n).
That was it, did not realize it had been deprecated. Thank you.
Deprecated or not, the reason is that random_shuffle() uses rand(), which must, as with any PRNG, be properly seeded before you use it.

Even if you use shuffle(), unless you seed your PRNG, you'll get the same sequence of numbers out of it each time (though not necessarily the same sequence you'd get as from rand()).

Hope this helps.
Got it, not seeding it correctly. It does help, thanks.
Topic archived. No new replies allowed.