I'm trying to generate 1024 random numbers, staying within the first half of the ASCII table (everything < 127) but not including anything below 33. Basically I want to stay with characters which are displayable while staying away from the ones which only "display" in a console window.
The following works, but there is a weird... thing. I can't explain it in more detail other than the number generated is occasionally 3435973836. No matter how I twist this, that number appears no less than 100 times.
Code:
int main (int argc, char **argv)
{
char data[1024];
char c = 0;
unsigned debug_data[1024];
memset (data, 0, 1024);
srand ((unsigned) time (0));
for (unsigned i = 0; i < 1024; i++) {
c = rand () / (RAND_MAX / 127 + 1); // stay within lower 127?
if (! (c > 33)) continue;
key[i] = c;
debug_data[i] = (unsigned) c;
}
return 0;
}
I build up debug_data so I can see the ASCII values for each character it outputs. That weird random value appears randomly...
Is there anything glaringly wrong with it?