I have this code and I want to make it so that each number can be outputted only 4 times each.

I have this code and I want to make it so that each number can be outputted only 4 times each.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//c++ version 5.11
#include <iostream>
#include <stdlib.h> 
#include <time.h> 
using namespace std;

main ()
{
	srand(time(NULL));
	
	//declaring variables
	int random, i;
	int deck [13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

	for (i =0; i<=20; i++)
	{	
		random = rand() %12 +1; //generating random #
		
		cout<<"\nYour random # is: "<< deck[random] ;	
		
	}
}
Looks like you trying to create and shuffle a deck. First thing I would do is create the deck with two nested loops. The outer loop can handle the suits (the 4 you mention) and the inner loops the rank (1-13).

After you have created it, then shuffle it. There is a std::shuffle function that works on most of the standard containers. You can look into std::array which would behave similar to your basic array here.
Topic archived. No new replies allowed.