
/******************************
 * simulate exponential r.v.s.*
 * Math 5040 - Fall 2002      *
 ******************************/
 
#include<stdio.h> 
#include<stdlib.h>
#include<time.h>
#include<math.h>

int main(void)
{
float U,X;
int i;
int n=0; 
int m=0;
float S=0;

/* set seed for pseudo random number generator
 * using the system time 
 */

srand(time(NULL));
	
for(i=0;i<100;i++){

	/* U is uniform r.v. */
	U = (float) rand()/(float) RAND_MAX;
	/* X is exponential r.v. */
	X = -log(1-U);
	if (X<1) {
		n = n + 1; /* n increments if X < 1 */
		}
	if (X>2) {
		m = m + 1; /* m increments if X > 2 */
		}
}
		
printf("fraction less than 1 is %f\n",n/(float) 100);
printf("fraction greater than 2 is %f\n",m/(float) 100);

return 0;
}