Randomizing

Hi, I am trying to code a concentration matching game and so far I have used srand() for randomizing numbers and I need to have 5 numbers each having a double or a "match". So far I use
 
  1 + (rand() % 5)

I already put 'srand(time(NULL));' at the beginning of main. So the above little bit of code can randomize between 1 and 5 fine, but when I put it to a loop, it will keep randomizing, but sometimes using the same number MORE than twice. How can I limit each digit to one match/double??

Hopefully this makes sense..
WAIT JUST KIDDING, I have to make it an array. Can someone help me start this out them? 5 numbers of 1 match each, for a total of 10 random numbers.
You are on the right track. What is it that you have problem with now? Creating the array? Filling it with values? Randomizing it?
I think an array would be the best choice, yeah. I have no idea how to make an array but I've done a checking code before using a while statement so maybe this....might help?

Here's an excerpt from my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{


	int answer, correct_answer=0, question, question_number=0, limit=0, t=0, t2=0;
	
	srand((unsigned)time(0));
	while (question_number !=10) 
	{
//here's where it checks to see if there's a repeat
// and if there is, then it  randomizes a new one to replace it
		question = 1 + rand() % 10;
		t = question;
		while (t== t2)
		{
			question = 1 + rand() % 10;
		}
		t2 = t;
		
		cout << "\nEnter the number that corresponds to the item shown: ";


Last edited on
Thanks! And I need 5 numbers to have only one double. So within an array of 10 values, I need to have numbers 1-5 each having only one double. How can I make it so there are not more than 2 copies?

example of what I need:
4, 3, 5, 2, 5, 3, 4, 2, 1, 1

example of what happens:
2, 4, 5, 2, 4, 4, 3, 1, 2, 2

Do you have to use an array or can it be a vector? If you use a vector, you can use random_shuffle().
http://www.cplusplus.com/reference/algorithm/random_shuffle/

You can then simply fill it with pairs of numbers from 1 to 5 in a for loop and shuffle it.
1
2
3
4
5
6
7
for(int i = 1; i <= 5; ++i)
{
	// add 2 of each number to the vector
	avector.push_back(i);
	avector.push_back(i);
}
random_shuffle(avector.begin(), avector.end())
Last edited on
No, my prof. wants it to be in arrays. Okay so here's what I want to do:

1.) out put place numbers 1-10

2.) out put random numbers 1-5 and their matching pair.

3.) make line # 2 (one step above this) all convert to 'x' and have user guess.

4.) each time they guess, the x's may turn to see if they match.

5.) if matches, keep them flipped.

I want to use arrays but I can't seem to keep them from repeating the random numbers more than twice. And getting user input to match the card flipped and remained flipped if correct. Ahhh I'm so overwhelmed!
Is this supposed to be in a console because I do not understand this part:
1.) out put place numbers 1-10

2.) out put random numbers 1-5 and their matching pair.

3.) make line # 2 (one step above this) all convert to 'x' and have user guess.


Is it supposed to look like this:
1 2 3 4 5 6 7 8 9 10
x x x x x x x x x x 


Anyway, you can still fill an array the same wayforget this, right indexes wrong numbers
1
2
3
4
5
6
for(int i = 1; i <= 10; i = i + 2)
{
	// add 2 of each number to the array
	anarray[i-1] = i;
	anarray[i] = i;
}

just declare one with the same elements.

You would then need to fill another 10 element array with random numbers between 1 and 10 and loop through it each time you add a number making sure there are no duplicate numbers. Then if the user picks picks 2 and 5, for example, you would use the values of elements 2 and 5 from the random array to see if they matched in the answer array. It would also take a third array to store the x's so you could replace them on the screen.

I imagine that confused you even more though. There seems to be a shuffle function in C++11 that works on arrays which would allow you to do the same thing I said before, but I am not familiar with it.
http://www.cplusplus.com/reference/algorithm/shuffle/
Either way you will still need an array of x's that you can replace with the numbers for output.

It is really not that hard, just a round about way of doing it.
Last edited on
Registered users can post here. Sign in or register to post.