/* * Try to use rand() to mimic random() behaviour * Test for uniform distribution of values * * With TOP=100 you get 20893 rand() results you should discard * (out of 10000000) */ #include #include #define TOP 100 #define N 100000 #define TRIES TOP*N int v[TOP]; int my_random(int n); int main(int arch, char **argv) { int i,j; int c1, c2; int gc=0, gn=0; for( i=0; i < TRIES; i++) { v[my_random(TOP)]++; } for( i=0; i < TOP; i++) { printf("%0.2d\t%d\t%f\n",i,v[i], (float)v[i]/N); } return 0; } /* return values so 0 <= x < n */ #if defined(TRIM) int my_random(int n) { int value,aux; if((n<=0)||(n>RAND_MAX)) return(-1); aux= RAND_MAX - ( RAND_MAX % n + 1 ) % n; do value=rand(); while( value > aux); return(value%n); } #elif defined(MOD) int my_random(int n) { return ( rand() % n); } #endif