L-22 MCS 260 Wed 10 Oct 2001
/* L-22 MCS 260 Wed 10 Oct 2001 : floating random number generator
In simulations we need a random distribution of floating-point
numbers. The program below shows how to generate a "random"
floating number between 0 and 1, using the integer random number
generator. We show how to convert ("cast") integers to floats.
We also show when this conversion happens implicitly
and when it does not automatically happen.
Observe the difference in precision between float and double. */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
int r;
double x,y,z;
srand(time(NULL));
r = rand(); /* r is in [0,RAND_MAX] */
printf("random integer : %d\n", r);
x = r/RAND_MAX; /* wrong: r/RAND_MAX == 0 */
printf("without a cast : %.15lf\n", x);
x = (float) r/RAND_MAX; /* explicit cast to float */
printf("random float : %.15e\n", x);
y = (double) r/RAND_MAX; /* explicit cast to double */
printf("random double : %.15e\n", y);
z = r; /* implicit conversion */
z = z/RAND_MAX;
printf("random double : %.15e\n", z);
return 0;
}
We can use our experience with the program above to implement an unfair coin :
/* L-22 MCS 260 Wed 10 Oct 2001 : unfair coin
We use the floating point random number generator to build an
unfair coin toss : in 62% of the cases, 1 should come up. */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
int r,n,i;
double x;
srand(time(NULL));
printf("Give number of coin tosses : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
r = rand(); /* r is in [0,RAND_MAX] */
x = (float) r/RAND_MAX; /* explicit cast to float */
if (x <= 0.62)
putchar('1');
else
putchar('0');
}
putchar('\n');
return 0;
}
The next application also involves conversion from integer numbers
to floating-point numbers.
/* L-22 MCS 260 Wed 10 Oct 2001 : interest calculation
Compile this program like
gcc -o interest interest.c -lm
to link with the mathematical functions */
#include<stdio.h>
#include<math.h>
int main(void)
{
double principal; /* principal amount to invest */
double rate; /* fixed rate of return */
int years; /* number of years */
double amount; /* final amount after number of years */
int i; /* controls the for loop */
printf("\nWelcome to the interest calculator.\n");
printf(" Give principal amount : "); scanf("%lf", &principal);
printf(" Give interest rate percentage : "); scanf("%lf", &rate);
printf(" Give number of years : "); scanf("%d", &years);
printf("\nInvesting $%.2lf at %.2lf%c for %d years\n",
principal, rate, 37, years);
rate /= 100; /* convert 3.43% to 0.0343 */
amount = principal;
for (i = 0; i < years; i++)
amount = amount*(1+rate);
printf(" yields $%.2lf when compounded yearly (loop calc).\n", amount);
amount = principal*pow(1+rate,years);
printf(" yields $%.2lf when compounded yearly (pow calc).\n", amount);
amount = principal*pow(1+rate/4,4*years);
printf(" yields $%.2lf when compounded quarterly.\n", amount);
amount = principal*pow(1+rate/12,12*years);
printf(" yields $%.2lf when compounded monthly.\n", amount);
amount = principal*pow(1+rate/365,365*years);
printf(" yields $%.2lf when compounded daily.\n", amount);
amount = principal*exp(rate*years);
printf(" yields $%.2lf when compounded continuously.\n\n", amount);
return 0;
}