The basic problem is that rand
is not generating real random numbers: In computing, to generate random numbers is a very complex problem, and the solution is (generally) to use pseudo-random number, which seem random but each number is built based on the previous one:
e.g.
n0 = 5 (seed = 5)
n1 = ((n0*10+1) % 7) = 2
n2 = ((n1*10+1) % 7) = 0
n3 = ((n2*10+1) % 7) = 1
n4 = ((n3*10+1) % 7) = 4
...
Where the 10, 7 and 1 have to be carefully selected to get seemingly
random numbers.
https://en.wikipedia.org/wiki/Linear_congruential_generator
srand
set the first seed value, and each new random use the previously generated random value as seed.
Setting a specific value to srand
will generate the exact random numbers serie again and again which is useful for testing. To make something more "random" the seed is usually set to the current time, which is expected to change over time.
The problem in your code is: your loop is too fast in order to get different time values each loop, that mean you set several times the seem value to the seed, generating the same random result.
As suggested, you need to set the seed outside the loop, so each random take the previous random result as seed and generate a seemingly new random number.
srand((unsigned int) time(NULL)); // Seed
while(1) {
double chance = random() / (double) RAND_MAX;
if (chance <= 0.95)
printf("All ok.\n");
else
printf("ERROR\n");
}