I'm building a really basic monte carlo simulator in C++ and have had a real problem with random numbers. The issue is this: I seem to be able to create a generally random sequence of value using the rand() function in conjunction with time, but it seems that when I try to throw that value into a function or anything that manipulates the variable, it just ceases to work anymore.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
usingnamespace std;
int random ()
{
return (rand() % 2)+1;
}
int main (int argc, char* argv []) {
srand (time (0));
double pay_in = 1.0, hld = 0.0;
int trials = 10;
for (int N = 0; N!=trials; N++)
{
hld = random();
if (hld = 1)
{
cout << "J\t" ;
}
elseif (hld = 2)
{
cout << "F\t";
}
cout << random() << endl;
}
}
When I run this particular one, I'll get the random number, but the program doesn't appear to pick it up and enter it into the if function, and as a result I just get a line of J's wt ha random number afterwards. I've perused forums and guides a great deal without any luck so any pointers here would be fantastic.