# Random letters

• 06-09-2002
Unregistered
Random letters
How would I use rand() to use letters other than numbers? I only want a-z and the first letter to be capitol.
• 06-09-2002
Zeeshan
char unknown = (rand() % 26) + 'a';
putc(unknown);
• 06-09-2002
Magos
Don't forget to seed the random number generator with srand(...);
• 06-09-2002
Flikm
you just have to get a random number between 0 and 255 for a signed char. -128 and 127 for unsigned (are those numbers right?).
• 06-09-2002
frenchfry164
just make it come up with a random number from 1 to 26. But I guess I'm late, aren't I? :p
• 06-09-2002
hostensteffa
i'm sure one of these other replys would work better but a really simple way to do it is to use

Code:

``` #include<stdio.h> #include<iostream.h> int main() int mynumber;  // declare the variable char myletter[2];    //can't think of the code but srand() would go here mynumber= rand()%25 + 1;  //get a random number between 1 and 26 switch(mynumber) {     case 1:     {             strcpy("A" , myletter);    copy 'A' into myletter             break;       }    //end case       case 2:       {             strcpy("B", myletter);             break;       } }  //end switch    return 0; }//end main```
once again this code is probably the simplest and most ineffecient way to do this but should work. Hoipe i was of some help.
• 06-09-2002
Flikm
oh lord, that is rather inefficient ... no use in using switch ... and instead of using an array, use just one char ...
• 06-09-2002
cozman
something like this?
if you want the first letter to be capitalized i assume you are looking for a completely random word?

void randomWord(char *word, int length)
{
word[0] = (rand() % 26) + 'A';

for(int i=1; i < length; i++)
word[i] = (rand() % 26) + 'a';
}
• 06-09-2002
Vicious
you could store letters and numbers or what ever in an array...

Code:

``` char randstuff[5] = {'A', '>', '2', 'r', 'R', '6'}```
then get a random number between 0 and 6 is it?

any way assign a variable like `i' or some thing to the random number

then
Code:

`cout << randstuff[i];`

note: forgive me... im half asleep...