Using rand() with a set list of numbers

Hello, I am attempting to write a memory game program that will display 18 numbers {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9} in a random order. I am currently stuck on how I can clarify what those numbers are and how to make sure they are not displayed more than twice. In the code so far I have made it so I get 18 random numbers between 1 and 9. Here is what I have so far, any help would be great!

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void randomNumber(int number[]);

int main()
{
	int number[18];

	randomNumber(number);

	system("pause");

	return 0;
}

void randomNumber(int number[])
{
	srand((unsigned int)time(0));

	cout << fixed << left << setw(18) << "Numbers to guess " << " --> ";

	for(int i = 0; i < 19; i++)
	{
		number[i] = rand() % 9 + 1;

		cout << fixed << setw(2) << number[i] << " ";
	}

	cout << "\n" << endl;

	cout << fixed << left << setw(19) << "Cell numbers " << "--> ";

	for(int i = 0; i <= 18; i++)
	{
		cout << fixed << setw(3) << i;
	}
}
You could use random_shuffle for this purpose.
http://cplusplus.com/reference/algorithm/random_shuffle/

I think this would suffice:
1
2
int array[17] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9};
std::random_shuffle(array, array+17);
Think about this a different way. You want to assemble a set of numbers, and then shuffle them, and then output them.

C++ comes with helpful functions such as random_shuffle.
Topic archived. No new replies allowed.