Sunday, August 22, 2004

I started working through the simulation exercises in chapter 3 of Ross's Simulation. Here is the C++ program for simulating the random variable exp(exp(x)) between 0-1.

//
// A C++ program For integrating a continous random variable exp(exp(X)) between 0-1 and finding its expected value or mean.//
#include
int Kvalue;
float Svalue;
int Seed;
float U;
int countK;
float Expected;



int main()
{


std::cout << "Enter integer for random seed: ";
std::cin >> Seed;
void srand(unsigned int Seed);


std::cout << "Enter integer for length of Simulation: ";
std::cin >> Kvalue;
countK=0;
Svalue = 0;
while (Kvalue >= countK)
{

U= (float) ( rand() / (RAND_MAX + 1 ) );
Svalue= Svalue + exp(exp(U));


countK++;
}


Expected= Svalue/Kvalue;
std::cout << "Expected Value = ";
std::cout << Expected;
std::cout << "Svalue = ";
std::cout << Svalue;
std::cout << "Kvalue = ";
std::cout << Kvalue;
return 0;
}


No comments: