I want to collect the \"best\" way to generate random numbers on all four types of intervals in one place. I\'m sick of Googling this. Search results turn up a lot of crap.
The following is the (very crude) test I am using to find basic errors in the numbers being generated. It is not intended to show the generated numbers are good but that they are not bad.
#include
#include
#include
int main(int argc, char *argv[]) {
long double x1,x2,x3,x4;
if ( argc!=2 ) {
printf("USAGE: %s [1,2,3,4]\n",argv[0]);
exit(EXIT_SUCCESS);
}
srand((unsigned int)time(NULL));
printf("This program simply generates random numbers in the chosen interval\n"
"and looks for values on the boundary or outside it. When an\n"
"allowable boundary is found, it reports it. Unexpected \"impossible\"\n"
"values will be reported and the program will terminte. Under\n"
"normal circumstances, the program should not terminate. Use ctrl-c.\n\n");
switch ( atoi(argv[1]) ) {
case 1:
/* x1 will be an element of [0,1] */
printf("NOTE: Testing [0,1].\n");
while ( 1 ) {
x1=((long double)rand()/RAND_MAX);
if ( x1==0 ) {
printf("x1=0 ENCOUNTERED.\n");
} else if ( x1==1 ) {
printf("x1=1 ENCOUNTERED.\n");
} else if ( x1 < 0 ) {
printf("x1<0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x1 > 1 ) {
printf("x1>0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
}
}
break;
case 2:
/* x2 will be an element of [0,1) */
printf("NOTE: Testing [0,1).\n");
while ( 1 ) {
x2=((long double)rand()/((long double)RAND_MAX+1));
if ( x2==0 ) {
printf("x2=0 ENCOUNTERED.\n");
} else if ( x2==1 ) {
printf("x2=1 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x2 < 0 ) {
printf("x2<0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x2 > 1 ) {
printf("x2>0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
}
}
break;
case 3:
/* x3 will be an element of (0,1] */
printf("NOTE: Testing (0,1].\n");
while ( 1 ) {
x3=(((long double)rand()+1)/((long double)RAND_MAX+1));
if ( x3==1 ) {
printf("x3=1 ENCOUNTERED.\n");
} else if ( x3==0 ) {
printf("x3=0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x3 < 0 ) {
printf("x3<0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x3 > 1 ) {
printf("x3>0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
}
}
break;
case 4:
/* x4 will be an element of (0,1) */
printf("NOTE: Testing (0,1).\n");
while ( 1 ) {
x4=(((long double)rand()+1)/((long double)RAND_MAX+2));
if ( x4==0 ) {
printf("x4=0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x4==1 ) {
printf("x4=1 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x4 < 0 ) {
printf("x4<0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
} else if ( x4 > 1 ) {
printf("x4>0 ENCOUNTERED. Abnormal termination.\n");
exit(EXIT_FAILURE);
}
}
break;
default:
printf("ERROR: invalid argument. Enter 1, 2, 3, or 4 for [0,1], [0,1), (0,1], and (0,1), respectively.\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}