Originally Posted by
quzah
(rand() * 201), on anything more than (RAND_MAX/201) is going to end up with an overflow.
That's fairly obvious. I was careful to avoid doing that by storing the fraction first.
There's no overflow here:
Code:
#include<stdio.h>
#include<stdlib.h>
int main( void )
{
int i;
double n;
double frac = 201.0 / ((unsigned int)RAND_MAX + 1);
printf( "denom = %u\n", (unsigned int)RAND_MAX + 1 );
printf( "frac = %e\n", frac);
for( i = 10000000; i > 0 && i < RAND_MAX; i+=10000000 ) {
n = (int)(i * frac) / 100.0 - 1;
printf( "%f\n", n);
// printf("%f\n", (int)(i * frac) / 100.0 - 1);
}
n = (int)(RAND_MAX * frac) / 100.0 - 1;
printf( "finally %f\n", n );
return 0;
}