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.
int position() //Randomly generates a starting position
{
int position;
if (random() < 30)
position = 1;
elseif (random() < 60)
position = 2;
elseif (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;
elseif (evaluator < 60) position = 2;
elseif (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();
elseif (evaluator == 2) distance = 50 + threeDiceOfFive();
elseif (evaluator == 3) distance = 100 + threeDiceOfFive();
else distance = 150 + threeDiceOfFive();
return distance;
}
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:
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).
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
usingnamespace 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;
elseif (evaluator < 60)
position = 2;
elseif (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;
elseif (evaluator == 2)
distance = 50 + dice;
elseif (evaluator == 3)
distance = 100 + dice;
else
distance = 150 + dice;
return distance;
}
int main()
{
srand ((unsignedint)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!