C++ only Generating random colors Write a program that generates a different lis
ID: 3602841 • Letter: C
Question
C++ only
Generating random colors
Write a program that generates a different list of 100 random RGB color values using hexadecimal notation. The program should write something like the following into cout.
324AF3
6A3125
... 98 additional RGB values
To solve this problem you need to generate strings with 6 characters where each character is randomly chosen from the following set of symbols.
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }
To make the choice random, you need to use the modulo operator (%). Specifically, use the expression rand()%16 to get a random integer between 0 and 15.
You are required to solve this problem by defining a loop within a loop. In fact, with the language features of C++ covered until this point, the solution to this problem requires a loop within a loop. If you know how to solve the problem without a nested loop, then you are required to solved it with a nested loop anyway, because learning how to write nested loops is the main learning objective of this exercise.
The program should generate a different list of color values each times it runs. To get this result, you need to seed the random number generator with a new value each time the program runs. One easy way to do this is to use the current time in seconds as a seed value. The following code shows how to do this.
#include <cstdlib>
...
int main()
{
srand(time(0));
...
return 0;
}
Explanation / Answer
output:
5E944F
D7D320
BE46C2
DE84C4
955750
90B591
61E81A
A0B955
63C96D
5ED9C5
9EB402
CCB6BB
43B78B
19040E
960405
3875CC
58C9C3
EB65BC
C30E7B
03831D
41C392
9279F6
D7201E
B84403
F5607C
8241A4
12E3F9
87F77C
A93D22
78930D
2DE75B
CBFB54
9936B1
C9C5FA
8CDC94
C40D61
09D451
26BA2B
422C7D
C16307
AC2BD8
7A4526
3A5ED1
C3FB3A
EAAE9F
A2A7C4
90D80B
7F482B
74B669
606C5E
C27741
A77633
5EE9D9
4B36AB
6B973C
83E4BE
7B4ABF
8D23CB
C68277
4F863D
595B84
9C91A0
6A5411
862EEF
63FE5D
E688A5
8F90F2
E75619
6D8786
44BBF3
04781B
B9C7A3
43683A
BAEAEE
4959AA
D36CDD
CBDC7A
F2A672
E7B3D4
B21F84
CDD2A2
C8A7D8
66F2A3
28E0F6
827C44
EFD556
7AE100
3E8FF7
D983F3
74A026
00DCD1
F1A164
2DA237
28C71D
E86BB1
28061F
8375F7
2C616D
80A912
DBAFF5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.