I know how to generate numbers like 1-20, 1-1000 etc...
but Im trying to figure out how to generate random letters from A-Z?
any help please?

define a string literal

const char* const a_to_z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

generate a random integer ri in the range 0 to 25 (inclusive).
use a_to_z[ri] as the random letter.
note: *do not* do this: char( 'A' + ri )

hmm i dont seem to get it. btw im new to C++ :P

heres the code that I used to generate 3 random numbers

#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main()

{

unsigned seed = time (0);
srand (seed);

cout << rand() << " " << rand() << " " << rand() << " " << endl;

system("pause");
return 0;

}

where do i put the codes i need to generate letters instead?

Edited 3 Years Ago by happygeek: fixed formatting

i cant seem to get it lol eek!
can you just hook me up with the code?

note: *do not* do this: char( 'A' + ri )

Vijayan is saying this because it is not guaranteed that the character set will have the alphabetical characters 'A' through 'Z' or 'a' through 'z' represented by a contiguous block of integers. However, it is very likely. Some character set uses something like the ordering AaBbCc...Zz , which does not actually provide a sufficient lexicographical ordering, and EBCDIC has letters all over the place. But char('A' + ri) will probably work for you, and if it doesn't, you deserve so for having used such a bizarre character set. :D

Comments
Nice clear response
int main()
{
    int n = rand() % 26;
    char c = (char)(n+65);
    cout << c << "\n";
    return 0;
}
Comments
Thanks, You were very clear and percise, it was very easy to understand.

i cant seem to get it lol eek!
can you just hook me up with the code?

We don't generally "just hook you up with the code." You make an attempt, show us your work, and we'll help you sort it out. You've been give two (or three) suggestions on how to solve your problem.

Vijayan is saying this because it is not guaranteed that the character set will have the alphabetical characters 'A' through 'Z'or 'a' through 'z' represented by a contiguous block of integers

Although correct, I have never seen such an operating system, but there could be one or more very obscure os's that have that quality.

Vijayan is saying this because it is not guaranteed that the character set will have the alphabetical characters 'A' through 'Z' or 'a' through 'z' represented by a contiguous block of integers. However, it is very likely. Some character set uses something like the ordering AaBbCc...Zz , which does not actually provide a sufficient lexicographical ordering, and EBCDIC has letters all over the place. But char('A' + ri) will probably work for you, and if it doesn't, you deserve so for having used such a bizarre character set. :D

Good points. It's been soooo long since I've seen EBCDIC, I'd forgotten that it isn't in contiguous sequences (only dealt with any such system for one two semesters, about 20 years ago).

You can always just use the ascii values of the letters you want, combine it with your knowledge of random numbers and presto.

int Random(max, min) -> some random number function

int main
{
cout << char(Random(60, 40));
return 0;
}

im not too sure the exact ascii values of the alphabet, but you can easily look up a chart on google. Good luck!

Although correct, I have never seen such an operating system, but there could be one or more very obscure os's that have that quality.

Which is why you can ignore this advice. I've never seen (at least in the past 25 years) a system that a student uses that has a non-contiguous alphabet.

This article has been dead for over six months. Start a new discussion instead.