Colour

Hi, im wondering how to make randomly type letters.
Like this for a example
----------------------------------------------------------------------
int A;
A = 1;
while (A == 1) {

cout
srand ( time(NULL) );
printf ("Random number: %d\n", rand() % 100);
srand ( 1 );
}
----------------------------------------------------------------------
But that is a random number generator up to 100. But how do i make it throgh A-Z or even words?

Please Reply :)
closed account (4ET0pfjN)
One way I can think of:

string alphabets = {'a','b','c','d','e','f',g','h','i','j','k','l','m','n','o','p' /*etc*/};

so now you can mod 26 which will give you the range of indices in the above array: so from 0 - 25 siince whatever number mod divisor is b/t: 0 to (divisor - 1).

I'm assuming you want to different colours usiing hexidecimal coloring scheme
which is 0x(A-F)(0-9).
1
2
3
int A;
A = 1;
while (A == 1)


You can change that into:

while(1)

Also, you can:
1
2
3
4
5
6
7
8
9
10
11
12
// If you know how to use the "For" loop:
for(char i = 'A'; i <= 'Z'; i++)
{
    printf("%c, ", i);
}
// If you don't know how to use the "For" loop:
char i = 'A';
while(i <= 'Z')
{
    printf("%c, ", i);
    i++;
}


Will print each character like:
A, B, C, D, E, (...)


Also try to call srand just once, in the beginning of your program like:
1
2
3
4
5
srand(time(0));
while(1)
{
    printf("Random Number: %d\n", rand() % 100);
}
Last edited on
Topic archived. No new replies allowed.