4

I have a std::vector that I need to shuffle. It is only ~20 members in size. It also needs to produce a different shuffle every time the program is run.

Right now I am using random_shuffle, however, it gives the same result every time the program is run. I tried that srand(unsigned(time(NULL))); that was suggested in this thread, however, that didn't work on my platform.

If possible, I want to use only standard code.

Edit: Here is my implementation:

vector<Tile>gameTiles;
gameTiles.push_back(Tile(0,0));
gameTiles.push_back(Tile(0,1));
gameTiles.push_back(Tile(0,2));
gameTiles.push_back(Tile(0,3));
gameTiles.push_back(Tile(0,4));
//etc. ~20 member

random_shuffle(gameTiles.begin(), gameTiles.end());
Community
  • 1
  • 1
  • Did you do it like in this [example](http://www.cplusplus.com/reference/algorithm/random_shuffle/) ? I am pretty sure it works and is all standard. – luk32 Sep 22 '12 at 05:18
  • 1
    Also, it would be nice to see your code. –  Sep 22 '12 at 05:22
  • 1
    You're trying to shuffle 2 things and consider it as not working? – Rapptz Sep 22 '12 at 05:31
  • @Rapptz I'm sorry if I didn't make it clear, the implementation I posted above is pseudocode for the sake of brevity. As I said in the question, the actual vector size is ~20. –  Sep 22 '12 at 05:34
  • 1
    For future reference: when, in the comments, someone asks to see your code(they shouldn't have to ask), don't show pseudocode. Show your actual code. If it's too long, then try to get it down to a short, complete example that demonstrates the problem. Make sure that it's something you actually compiled, (or tried to compile, if the problem you're asking about is a compiler error). – Benjamin Lindley Sep 22 '12 at 05:44
  • @BenjaminLindley Thank you for the reminder, my fault, I should have have posted the actual code. I will fix it now –  Sep 22 '12 at 05:50
  • 1
    Hate to be harping on you, but: The example is better, but still, it should be a complete example. This provides (at least) two benefits. 1) The people trying to answer your question can quickly throw your code in a compiler and test it out themselves. 2) If you're asking about a problem with your code, it's unlikely that you are aware of what is causing the problem. By only showing part of your code, you may be leaving out the very thing that's causing the problem, which would make your question very difficult to answer. – Benjamin Lindley Sep 22 '12 at 14:11
  • 1
    And, like I said, if the code is too long, then try to narrow the problem down. If that's too difficult, then fine, incomplete code is okay. But, in this case, you probably should have been able to create a minimal example, like [this](http://ideone.com/lm5pG), and it would have demonstrated the problem. – Benjamin Lindley Sep 22 '12 at 14:15
  • @BenjaminLindley Hey you're not harping on me, I didn't know there was an issue with the way I was posting code. I definitely want to help the people who are helping me. What specifically is the problem here though? Do you want the rest of the text of the function that I am doing the shuffling in? Or do you want the text of `Tile`. The class that the `gameTiles` is long, and mostly unrelated. `Tile` is also very long, about 500 lines. I didn't see how either one was relevant. Should I be putting up the constructor for `Tile` this is using? –  Sep 22 '12 at 14:17
  • @BenjaminLindley oh okay, I understand what you mean now. I see how that is much easier for people to compile quickly and test out. Thank you –  Sep 22 '12 at 14:19

2 Answers2

12

If srand(unsigned(time(NULL))); doesn't help, then your implementation must not be using the standard rand() as its random number generator. In that case, there is an alternative version of random_shuffle that takes a custom random number generator. You could just pass a wrapper around rand(), like this:

int MyRand(int n)
{
    return std::rand() % n;
}

// ...
std::random_shuffle(bar.begin(), bar.end(), MyRand);
// ...

If you want something with a more uniform distribution, look into the C++11 <random> header.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Yup, it seems like it must not be using the standard `rand()`. The wrapper works perfectly for this project thought. Thanks for the response. –  Sep 22 '12 at 05:37
1

Well the implementation you have give cannot work (you named a type foo and used the same literal as a variable). Anyways try something like this

// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;

int main(void){
  srand(0)
  vector<Tile>gameTiles;
  gameTiles.push_back(Tile(0,0));
  gameTiles.push_back(Tile(0,1));
  gameTiles.push_back(Tile(0,2));
  gameTiles.push_back(Tile(0,3));
  gameTiles.push_back(Tile(0,4));
  random_shuffle(gameTiles.begin(), gameTiles.end(), p_myrandom);
}

It will ensure random_shuffle uses standard rand and that it is initialized properly. It should give different results unless you execute the application twice in the same second, that would lead to using the same seed for rand. The code it right from here but there is anything to change, so i just used it.

luk32
  • 15,812
  • 38
  • 62