c++arraysrandomc++builderc++builder-6

Random_shuffle alternative in C++Builder6


As the title says, I'm trying to find a method to shuffle an array. C++Builder doesn't seem to have random_shuffle() and I don't know how to do it.

I can't find much about this program on google. To be more specific, I'm trying to do a memory matching game that has 16 pairs. I tried implementing something to give each card a random position (position 0, 1, 2, .. etc) but I don't know how to do it without having a value duplicated.

So the easiest method I see is creating an array a[]={0,1,2,...31} and shuffling the values somehow. Is it possible to do anything like this? If not, do I have any alternatives?

I hope my question is understandable.


Solution

  • The standard algorithm to shuffle an array is the Fisher-Yates algorithm. That is easy to implement and there are a number of examples on the web. However, if you are learning then it would be a good idea to program it from scratch rather than blindly copy someone else's code.