frank <> writes:
[..]
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(void)
> {
>
>
> int i;
> char c;
> srand((unsigned int)time((time_t *)NULL));
>
> i=rand();
> printf("i is %d\n", i);
> printf("i is %d\n", RAND_MAX);
> c=(char)i;
> printf("c is %c\n", c);
>
> return 0;
> }
Indentation?
None of the three casts in your program are necessary, and IMHO your
code would be improved by dropping them.
srand(time(NULL);;
...
c = i;
The second "i is %d\n" presumably is a typo for "RAND_MAX is %d\n".
If plain char is signed, the conversion of i from int to char (which
occurs with or without the cast) can produce an implementation-defined
result or raise an implementation-defined signal. If you want to
produce numbers within a specified range, you should do the necessary
arithmetic yourself.
If you want random lowercase letters, you can declare
const char letters[] = "abcdefghijklmnopqrstuvwxyz"
and index into the array with a random number in the range 0..25.
Section 13 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has
several questions about random numbers.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"