Here's a quick comparison of the different ways of scaling the output of rand()
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int y = 0;
for(; y < 5; y++) {
printf("Trial %d\n", y);
int r1 = 0, r2 = 0;
int x = 0;
for(; x < 10000; x++) {
if(rand() % 10000 >= 5000) r1++;
}
for(x = 0; x < 10000; x++) {
if((rand() * 10000) / (RAND_MAX + 1) >= 5000) r2++;
}
printf("Results for rand() %% 10000: %d\n", r1);
printf("Results for (rand() * 10000) / (RAND_MAX + 1): %d\n\n", r2);
}
return 0;
}
And the results:
Trial 0
Results for rand() % 10000: 4557
Results for (rand() * 10000) / (RAND_MAX + 1): 5090
Trial 1
Results for rand() % 10000: 4575
Results for (rand() * 10000) / (RAND_MAX + 1): 5036
Trial 2
Results for rand() % 10000: 4563
Results for (rand() * 10000) / (RAND_MAX + 1): 5041
Trial 3
Results for rand() % 10000: 4546
Results for (rand() * 10000) / (RAND_MAX + 1): 4976
Trial 4
Results for rand() % 10000: 4552
Results for (rand() * 10000) / (RAND_MAX + 1): 5028
It's clear that the scaling method is significant.