Sunday, August 15, 2004

I am reading this book today and yesterday: Ross, Sheldon. Simulation 2d (San Diego: Academic, 1997). I am trying to make the computer program from chapter 3 for integrating a random variable run a number of times in a loop using a psuedorandom number generator. The book has an example in BASIC. I am trying to write a similar program in C++. I have been searching the web for C++ syntax and so forth. It is not complete yet. I have the below code done so far but I am not returning a random number the right way yet.


Here is my code adopted from a simple C++ IO program from a basic learning C++ book using Quincy as my developer environment and of course gcc.



//
// A Simple C++ program For Integrating a random variable (X^2) and finding its expected value or mean.//
#include




int main()
{
int Kvalue;
float Svalue;
int Seed;
float U;
int countK;
float Expected;

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


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

float rand(U);
Svalue= Svalue + U*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: