### Help with Random Number Generation

Hello all,

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.

Generating random numbers:
 ``1234567891011121314151617181920212223242526`` `````` #include #include #include #include using namespace std; int random () { return (rand() % 4)+1; } int main (int argc, char* argv []) { srand (time (0)); for (int N = 0 ; N!= 10; N++) { cout << random() << endl; } } ``````

The below, however does not work:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` `````` #include #include #include #include using namespace 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" ; } else if (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.

use '==' instead of '=' in your if statements on lines 25 and 29
Last edited on
Here's a random number generator you could use, i'll even make it a little game for you
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) {//------------------------------------------Random Number Guessing Game--------------------------------------------- int random_number, random_number1, random_number2; int number; char answer; for(;;) { { srand(time(NULL)); int lowest = 1, highest = 20; int range = (highest-lowest) + 1; for(int index = 0; index < 20; index++) { random_number1 = lowest + int(range * rand()/(RAND_MAX + 1)); } } { srand(time(NULL)); int lowest = 1, highest = 20; int range = (highest-lowest) + 1; for(int index = 0; index < 20; index++) { random_number2 = lowest + int(range * rand()/(RAND_MAX + 1)); } } random_number = (random_number1 + random_number2) / 2; for(int guesses = 1;;guesses++) { cout << "Guess the number (1-20):" << endl; cin >> number; if (number > random_number) { cout << "You are too high" << endl; } if (number < random_number) { cout << "You are too low" << endl; } if (number == random_number) { cout << "You Win!!! You found the number in: " << guesses << " tries!" << endl; cout << "Do you want to play again? (y/n)"; cin >> answer; } if(answer == 'n') { break; } if(answer == 'y') { break; } } if (answer == 'n') { break; } if (answer == 'y') { answer = 'd'; } } system("PAUSE"); return 0; } ``````
Thank you bandicoot, that was the thing that actually fixed it. Very facepalm worthy.

Cool game greenleaf - I remember seeing a question involving that in my text book and it's an interesting problem!

Thanks again guys!
Topic archived. No new replies allowed.