Shuffling values help? (Random Numbers with NO repetition)

Hello, I'm writting a memory matching game for my final project of my C++ Procedural programing class..., I need to shuffle the cards for every game so that it won't do the same thing over and over again. I already built a random number generator, but I am having a lot of trouble making it only print out the numbers ONLY ONCE, so that they are shuffled... Any help will be GREATLY appretiated! Thank you!
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
#include <string>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

void gridEasy(void);
void randomizeTheme(string theme[]);

int _tmain(int argc, _TCHAR* argv[])
{       
	string theme[16] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p" };
	
	
	randomizeTheme(theme);
	for (int i = 0; i < 16; i++)
	{
		cout << theme[i] << endl;
	}
	system("Pause");
	return 0;


void randomizeTheme(string theme[])
{
	int r;
	srand(time(0));
	
	for (int i = 0; i < 16; i++)
	{
		string temp;
		r = rand() % 16;

		temp = theme[i];
		if (theme[i] != theme[r])
		{
			theme[i] = theme[r];
			theme[r] = theme[i];
		}
		
	}

}


By the way, My output comes our like this:


a
a
b
d
c
a
d
i
h
o
p
a
d
c
d


some repeat many times and some don't even come out...
I need them to be printed only once... but in random order...
Last edited on
Are you not allowed to use C++ functions std::random_shuffle or std::shuffle?
@Cubbi

Well, he didn't set any limitations, he really let us free, I guess as long as we understand what we are doing its fine...
Then it's just
1
2
3
4
5
6
#include <algorithm>
...
void randomizeTheme(string theme[])
{
	random_shuffle(theme, theme+16);
}

(with srand moved to main or to something static, so you don't execute it every time you call this function.). To understand how it works, see http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
Last edited on
It Works perfectly! Thank you so much! but just to make it clear that I understood this well, So the
random_shuffle()
function, when you put
theme
in the first place before the comma, it is reffering to the starting point of the array
 theme[0] 
and the last one after the comma
theme + 16 
refers to the endpoint or
theme[0+16] == theme[15] 
so that it re-arranges all the values in between right?
You are almost correct, AdrianV.

theme is a pointer to a string, and since this pointer happens to be the head of an array of 16 strings, the successive pointers can be encompassed with theme + 16.
almost: theme[0+16] is not theme[15]. it is the one-past-the-end pointer, which is used throughout C++ standard library to indicate the end of a sequence.
Last edited on
If your professor does not allow you to use the standard library, then you could implement the simple but effective Fisher-Yates shuffle (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void my_swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

// Fisher-Yates Shuffle
void my_shuffle(int a[], int n)
{
    for (int i = n - 1; i > 0; i--)
    {
        my_swap(a[rand() % (i + 1)], a[i]);
    }
}
Topic archived. No new replies allowed.