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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>


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.

Thank you in advance!

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>
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.