Having trouble getting random numbers to do what I want

So I'm very new to programming and trying to teach myself cpp. I think I'm getting the basics but I've run into a problem trying to implement a concept I want to simulate. What I'm trying to do is randomly generate a "position" (ignore the variable names and numbers, they're meaningless, I'm just using them for proof of concept) and then follow that with a randomly generated "distance" that is dependent on the position that is generated. I can get the random numbers generated without difficulty, but I can't figure out how to get "distance" to be dependent on "position". I think there is something wrong in how I'm calling position() in the distance() function but I don't see it.

Any suggestions would be greatly appreciated.

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
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

int random()  //Generates a random integer between 1 and 100
{
	int random = rand() / (RAND_MAX / 100 + 1);

	return random;
}

int threeDiceOfFive()  //Simulates the a roll of three five-sided dice
{
	int threeDiceOfFive= (rand() / (RAND_MAX / 4 + 1)) + (rand() / (RAND_MAX / 4 + 1)) + (rand() / (RAND_MAX / 4 + 1));

	return threeDiceOfFive;
}

int position()  //Randomly generates a starting position
{
	int position;
	
	if (random() < 30)
		position = 1;
	else if (random() < 60)
		position = 2;
	else if (random() < 90)
		position = 3;
	else
		position = 4;

	return position;
	}

int distance()  //Randomly generates a distance
				//distance <<SHOULD>> be dependent on the position generated above
{
	int distance;

	if (position() == 1)
		distance = 0 + threeDiceOfFive();
	else if (position() == 2)
		distance = 50 + threeDiceOfFive();
	else if (position() == 3)
		distance = 100 + threeDiceOfFive();
	else
		distance = 150 + threeDiceOfFive();
	
	return distance;
}

int main()
{
	srand ((unsigned int)time(0));

	int n = 1;
	
	while (n <= 10) { 
		cout << position() << " and " << distance() << endl;
		++n;
	}

	return 0;
}
You use the rand() too much time ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int position()  //Randomly generates a starting position
{
	int position;
	
	if (random() < 30)
		position = 1;
	else if (random() < 60)
		position = 2;
	else if (random() < 90)
		position = 3;
	else
		position = 4;

	return position;
	}


You call rand() (in random()) ) 4 times it will make you waste a lot of resource. and the position is likely to be 4 (the chance) because you get equal chance of having a number by using multiple rand().
To get a random integer number between x and y do the following:

 
int i = (rand() % (y-x) + x)


So if you want to return the sum of 3 * 5 sided dice:
int sum = (rand()%5) + 1 + (rand()%5+1) + (rand()%5+1);

Now, in your position function, don't call random() each time. The output will change each time you use it so you are very likely to get strange results. Use this instead so that random() is only evaluated once:

1
2
3
4
5
6
7
8
9
10
11
12
iint position()  //Randomly generates a starting position
{
	int position;
	int evaluator = rand()%100+1;
	
	if (evaluator < 30)          position = 1;
	else if (evaluator < 60)     position = 2;
	else if (evaluator < 90)     position = 3;
	else                         position = 4;

	return position;
}



In your distance function you have the same problem. The position() call will give you a different result every time it is used. Use it only once and evaluate the result. as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
int distance()  //Randomly generates a distance
				//distance <<SHOULD>> be dependent on the position generated above
{
	int distance;
	int evaluator = position();

	if (evaluator == 1)       distance = 0   + threeDiceOfFive();
	else if (evaluator == 2)  distance = 50  + threeDiceOfFive();
	else if (evaluator == 3)  distance = 100 + threeDiceOfFive();
	else                      distance = 150 + threeDiceOfFive();
	
	return distance;
}


Now you should get what you were expecting
Thanks for the help!

EDIT:
OK so I implemented that change and I end up with a different problem. In main() I'm asking the program to generate position() and distance() multiple times, hoping to get a different position() result, and hence a different distance() result, each time. Before I was getting different positions but distances not related to the position result. Now they are related but the position is the same every time. Any ideas? Here's the new code:

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
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

int threeDiceOfFive()  //Simulates the a roll of three five-sided dice
{
	int threeDiceOfFive= (rand() / (RAND_MAX / 4 + 1)) + (rand() / (RAND_MAX / 4 + 1)) + (rand() / (RAND_MAX / 4 + 1));

	return threeDiceOfFive;
}

int position()  //Randomly generates a starting position
{
	int position;
	int evaluator = rand() * 100 + 1;
	
	if (evaluator < 30)
		position = 1;
	else if (evaluator < 60)
		position = 2;
	else if (evaluator < 90)
		position = 3;
	else
		position = 4;

	return position;
	}

int distance()  //Randomly generates a distance
				//distance <<SHOULD>> be dependent on the position generated above
{
	int distance;
	int evaluator = position();
	int dice = threeDiceOfFive();

	if (evaluator == 1)
		distance = 0 + dice;
	else if (evaluator == 2)
		distance = 50 + dice;
	else if (evaluator == 3)
		distance = 100 + dice;
	else
		distance = 150 + dice;
	
	return distance;
}

int main()
{
	srand ((unsigned int)time(0));

	int n = 1;
	
	while (n <= 100) { 
		cout << position() << " and " << distance() << endl;
		++n;
	}

	return 0;
}
Last edited on
Your rand function use is still incorrect. It's int myRand = rand() % (y-x) +x; for a number in [x, y]. Mind the % (modulo operator), not the / (division operator) or the * (multiplication operator).
Ok, so I updated my random functions and still don't get what I need. Here's the updated code:

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
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

int threeDiceOfFive()  //Simulates the a roll of three five-sided dice
{
	int threeDiceOfFive= (rand() % (4 - 1) + 1) + (rand() % (4 - 1) + 1) + (rand() % (4 - 1) + 1);

	return threeDiceOfFive;
}

int position()  //Randomly generates a starting position
{
	int position;
	int evaluator = rand() % (100 - 1) + 1;
	
	if (evaluator < 30)
		position = 1;
	else if (evaluator < 60)
		position = 2;
	else if (evaluator < 90)
		position = 3;
	else
		position = 4;

	return position;
	}

int distance()  //Randomly generates a distance
				//distance <<SHOULD>> be dependent on the position generated above
{
	int distance;
	int evaluator = position();
	int dice = threeDiceOfFive();

	if (evaluator == 1)
		distance = 0 + dice;
	else if (evaluator == 2)
		distance = 50 + dice;
	else if (evaluator == 3)
		distance = 100 + dice;
	else
		distance = 150 + dice;
	
	return distance;
}

int main()
{
	srand ((unsigned int)time(0));

	int n = 1;
	
	while (n <= 100) { 
		cout << position() << " and " << distance() << endl;
		++n;
	}

	return 0;
}


Sorry, I may not be clear enough about what I'm needing to generate. What I am getting with the most recent code above is something like:

2 and 106
4 and 105
2 and 5
3 and 7
etc.

But what I need it to produce is something like the following, where position() changes and then distance() changes based on what position() returns. Something like this:

1 and 4
3 and 103
4 and 155
2 and 51
etc.

Again, thanks for all the help, I really appreciate it!
Last edited on
So...can anyone help me with this? I've tried everything and can't seem to get it to work.
Topic archived. No new replies allowed.