How to generate random numbers within an array

I am trying to do following game.

Read in the first user input die letter, which should be an upper-case R, G, or B, and use that letter to determine which die should be used. Use the random number generator rand() along with the mod (%) function to get a random value 0..5 representing which die face is chosen. Use the die color (R, G, or B) along with which face was randomly selected (0..5) to determine the numerical value. For instance, if the Red die was selected, the six values for each face are 1 4 4 4 4 4. The first face (face 0) corresponds to the first value (1), while all the other 5 face values all have the value 4.


My Question- How to generate the random numbers with in an array.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;



//This function is to display the Dice along with its faces
void display(int Red[6],int Green[6],int Blue[6])
{
	cout << "Red   : ";
	for (int i = 0; i < 6; i++)
	{
		cout << Red[i] << " ";
	}
	cout << endl;

	cout << "Green : ";
	for (int i = 0; i < 6; i++)
	{
		cout << Green[i] << " ";
	}
	cout << endl;

	cout << "Blue  : ";
	for (int i = 0; i < 6; i++)
	{
		cout << Blue[i] << " ";
	}
	cout << endl;
}


//This function takes in the user Input and randomly gives a number from the selected dice.
void play(int Red[6], int Green[6], int Blue[6])
{
	srand((unsigned)time(0));
	char player;
	player = toupper(player);
	
	int rand;
	
	cout << "Enter the die color (R G or B): " << endl;
	cin >> player;

	if (player == 'R')
	{
		for (int i = 0; i < 6; i++)
		{
			Red[i] = (srand() % 100) + 1;
		}
	}
}


//This is the main function
int main ()
{
	//Declaring the dice and their faces
	int Red[6] = { 1,4,4,4,4,4 };
	int Green[6] = { 2,2,2,5,5,5 };
	int Blue[6] = { 6,3,3,3,3,3 };


	//Calling the functions
	display(Red, Green, Blue);
	play(Red, Green, Blue);
  
	system("pause");

	return 0;
}
Last edited on
move line 39 to line 61, it only needs to happen once so best to do it as the program starts.

move line 41 o line 47, no use converting to upper case before the users enters their letter

remove line 43, it confuses with rand()

line 52 should call rand() to get a number, not srand() which seeds the generator.

line 70, add display(Red, Green, Blue); to see your results.

then you can extend play() to cover G and B also.

http://cpp.sh/5cty
@Jaybob66 I am supposed to get the result as follows.

The dice contain the following sides:
Red: 1 4 4 4 4 4
Green: 2 2 2 5 5 5
Blue: 6 3 3 3 3 3

Enter the die color (R G or B): R
Value: 4
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
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cctype>

const int NFACES = 6 ; // number of faces in a die

// return the value of a random face of the die
int random_face( const int die[NFACES] )
{
    const int random_face = std::rand() % NFACES ;
    return die[random_face] ;
}

// print the colour and faces of a die
void print( const char* colour, const int die[NFACES] )
{
    std::cout << colour << ": " ;
    for( int i = 0 ; i < NFACES ; ++i ) std::cout << die[i] << ' ' ;
    std::cout << '\n' ;
}

void display( const int red[NFACES], const int green[NFACES], const int blue[NFACES] )
{
    print( "  red", red ) ;
    print( "green", green ) ;
    print( " blue", blue ) ;
}

void play( const int red[NFACES], const int green[NFACES], const int blue[NFACES] )
{
    std::cout << "Enter the die colour (R G or B): " ;
    char clr ;
    std::cin >> clr ;
    clr = std::toupper(clr) ;

    if( clr == 'R' ) std::cout << random_face(red) << '\n' ;
    else if( clr == 'G' ) std::cout << random_face(green) << '\n' ;
    else if( clr == 'B' ) std::cout << random_face(blue) << '\n' ;
    else std::cout << "invalid colour\n" ;
}

int main()
{
    std::srand( std::time(nullptr) ) ;

    const int red[NFACES] = { 1,4,4,4,4,4 };
    const int green[NFACES] = { 2,2,2,5,5,5 };
    const int blue[NFACES] = { 6,3,3,3,3,3 };

    display( red, green, blue ) ;

    play( red, green, blue ) ;
}
Topic archived. No new replies allowed.