You can seed the random number generator like this:
srand((unsigned)time(0));
I'll explain: you seed rand with a number; it takes an unsigned int. So you cast the return value of the time() function to an unsigned int to seed rand. Then you can use rand() like this:
int random_integer = (int)(rand() % n + 1);
where
n is the maximum value you want to get. The + 1 is necessary if you want it to be
inclusive; e.g. it will generate random numbers up to and
including n.
Personally I find this is a pretty good method for generating random numbers, and have never used the method above:
1 2 3
|
int lowest_value = 0, highest_value = 256, range = (highest_value - lowest_value) + 1;
int random_number = lowest_value + (int)((range * rand()) / (RAND_MAX + 1.0));
|
Does that help?
Oh and if you want letters; try adding
'a'
(
with the inverted commas) for lower case and 'A' for upper case.
That works because the ASCII value of the letter a (the number it is stored as) is
almost always the same across every system. Where it isn't, however, because you used 'a' instead of it's numeric value (which tends to be 97 for lowercase, 65 for uppercase); it should work for any system that can print that character.
Hopefully I explained everything properly...