Hi,
When you initiate the rand function with randomize (in stdlib) it uses the current time / date to make the number random. If you use srand to initiate the number, you would need to write something like srand((NULL)time())...
To produce the random char I would do something like this:
char c[] = {"abcde.....ABCDE.....?.,:$!"0123456789"} // all valid chars
int n = strlen(c); // #chars in c[]
int i = rand(); // a random number from 0 to 32767
i = i % n; // i will now be in the interval from 0 to n-1
c[i]; // will be a random char
However, it is possible to improve the quality of the random number by replacing
i = rand();
with
do { i = rand(); } while (i >= n * (int)(32768 / n)) ;
Hope this helps.
|