random Gen

In this program the user inputs the number of squares (3-5) that he wants displayed on the graphics screen. The colors are supposed to be randomly colored with the selected colors I have in the program. The problem is that my squares are not random. The colors are always red, green, blue, light blue, and purple. Can someone please tell me why the squares are not being randomly generated?


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <ctime>
#include "graph1.h"

using namespace std;

void getNoColors(int* no_colors);
void setColors(int* colors, int no_colors);
void getDelay(int* secs);
void displayColors(int* colors, int no_colors);
void game(int* colors, int no_colors, int delay);
//Add prototypes here

int main()
{

  //Variable Declaration/Initialization
  const int MAX_COLORS = 5;
  int no_colors = 0;
  int colors[MAX_COLORS];
  
  //Display Graphics Window
  displayGraphics();

  //1. Get the # of colors
  getNoColors(&no_colors);

  //2. Set the colors 
  setColors(colors, no_colors);

  //3. Display the colors in the form of 75x75 rectangles
  displayColors(colors,no_colors);

  return 0;
}


//Implement functions here
void getNoColors(int* no_colors)
{
cout << "Input a number of colors:" ;
cin>>*no_colors;
}

void setColors(int* colors, int no_colors)
{
int i = 0;
int j= 0;
bool duplicate = false;

//initialize random # generator
	srand(time(0));
	
	//Generate random (but unique) colors
	for (i = 0; i< no_colors; i++)
	{
	do
	{
	colors[i] = rand()%no_colors;
	duplicate = false;
	
	//check for duplicates
	for (j= i-1; j >= 0; j--)
	{
		if (colors[i] == colors[j])
		{
		duplicate = true;
		break;
			}
		}
	}
	while(duplicate);
	}
}
void displayColors(int* colors, int no_colors)
{



int x=25;
for(int i=0; i < no_colors ; i++)
{
obj_no = drawRect(x,25,75,75);




for (int m=0; m< no_colors; m++)
{
	gout<<"Color #"<<(i+1);

	
	if (colors[m] == 0)
	{
	setColor(obj_no,255,0,0);
		}
	if (colors[m] == 1)
	{
		setColor(obj_no,0,255,0);
   }
	if (colors[m] == 2)
	{
		setColor(obj3_no,0,0,255);
	}
	if (colors[m] == 3)
	{
	setColor(obj_no,255,0,255);
	}
	if (colors[m] == 4)
	{
	setColor(obj_no,0,255,255);
	}
	x =x+100;

}
}
Topic archived. No new replies allowed.