How would I use rand() to use letters other than numbers? I only want a-z and the first letter to be capitol.
Printable View
How would I use rand() to use letters other than numbers? I only want a-z and the first letter to be capitol.
char unknown = (rand() % 26) + 'a';
putc(unknown);
Don't forget to seed the random number generator with srand(...);
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?).
just make it come up with a random number from 1 to 26. But I guess I'm late, aren't I? :p
i'm sure one of these other replys would work better but a really simple way to do it is to use
once again this code is probably the simplest and most ineffecient way to do this but should work. Hoipe i was of some help.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
oh lord, that is rather inefficient ... no use in using switch ... and instead of using an array, use just one char ...
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';
}
you could store letters and numbers or what ever in an array...
then get a random number between 0 and 6 is it?Code:
char randstuff[5] = {'A', '>', '2', 'r', 'R', '6'}
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...