Tue Apr 10, 2007 1:28 pm
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <unistd.h>
using namespace std;
double uniform(double randnum,double a, double b)
{
return (a+(randnum*(b-a)));
}
double exponential(double randnum,double lambda)
{
return (-lambda*(log(randnum)));
}
double normal(double mu, double sigma)
{
srand(time(NULL));
double random;
double tot=0.0;
for(int i=1;i<=12;i++)
{
random=(rand()%100)/100.0;
tot+=random;
}
return(mu+sigma*(tot-6));
}
int main()
{
double a;
double b;
double lambda;
double mu;
double sigma;
double val;
int choice;
cout << "Choose a distribution: " << endl;
cout << " - Press 1 for uniform\n"
<< " - Press 2 for exponential\n"
<< " - Press 3 for normal\n"
<< endl
<< endl
<< endl
<< endl
<< endl
<< "Choice:";
cin >> choice;
srand(time(NULL));
switch(choice)
{
case 1:
cout << "Enter lower limit for uniform distribution (a): ";
cin >> a;
cout << "Enter upper limit for uniform distribution (b): ";
cin >> b;
cout << "F(x)\t\t\t\tx" << endl;
cout << "------------------------------------" << endl;
for(int i=0;i<10;i++)
{
val=(rand()%100)/100.0;
cout << val << "\t\t\t\t" << uniform(val,a,b) << endl;
}
break;
case 2:
cout << "Enter the mean of exponential distrbution (lambda): ";
cin >> lambda;
cout << "F(x)\t\t\t\tx" << endl;
cout << "------------------------------------" << endl;
for(int i=0;i<10;i++)
{
val=(rand()%100)/100.0;
cout << val << "\t\t\t\t" << exponential(val,lambda) << endl;
}
break;
case 3:
cout << "Enter the mean of normal distribution (mu): ";
cin >> mu;
cout << "Enter the standard deviation of normal distribution (sigma): ";
cin >> sigma;
cout << "These are 10 normally distributed random variables" << endl;
for(int i=0;i<10;i++)
{
cout << normal(mu,sigma) << endl;
sleep(1);
}
break;
default:
cout << "Invalid choice" << endl;
}
return 0;
}
Sat Jan 26, 2013 6:08 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.