Hello everybody. I need to write a program for an assignment that has a few different parts, and right now I am trying to figure out one of the inner loops of a series of nested loops. The goal is to generate a random number between 1 and 5, and if the number is 1 through 3, the man walks backwards, and if it is 4 or 5, he walks forward (one step at a time for each). Each step moves him one block, and he starts on block 2. There are 8 blocks total. If he makes it to either block 1 or block 8, he stops there. I have to run a simulation over and over to see how often he ends up at block 8 (which represents his home), and also to tell how many blocks were traveled each time the simulation runs. My code seems to generate the same number for each trial (such as 1 block is traveled total over and over again, no matter how many times I run the trial), and so it seems I have a logic error somewhere, but I don't see it. Can anybody point me in the right direction? I have the proper preprocessor directives included, and please ignore the code segments that are commented out, I'm not working on that part of the assignment yet. The variables that are not used yet can also be ignored, those are for later.
int main() {
int location = 2; // Starting at block 2
int trialStartPoint = 2; // Starting at block 2, will increment to give a new starting point for the overall trials, up to the seventh block
int home = 0; // Number of times he ends up at home each trip
int blocksTraveled = 0;
int probability;
srand(time(NULL));
//while (trialStartPoint < 8) {
for (int i = 1; i < 6; i++) { // Multiple trials of the man's trip
while (location != 1 && location != 8) { // The man's trip, which ends when he reaches block 1 or block 8
probability = 1 + rand() % 5;
if (probability > 3)
location++;
else
location--;
blocksTraveled++;
if (location == 8)
home++;
} // End while loop
cout << "The man traveled " << blocksTraveled << " blocks on trip number " << i << endl;
} // End for loop
//trialStartPoint++;
//} // End while
return 0;
} // End main
When the while loop fails the for loop will execute line 31 until the for loop ends outputting the same information a total of 5 times.
Either reevaluate the while condition or do a check at the end of the for loop on the value of location and break out of the for loop if location equals 1 or 8.
After testing the program I made some changes that you may find useful.
probability = 1 + rand() % 5; is more commonly written as probability = rand() % 5 + 1;. The + 1 at the end keeps the number above zero. I believe that mathematically they are the same.
I changed this if (probability > 3) to if (probability >= 3) because every time I ran the program it always ended up at 1. This change allowed it to go to 8 on occasion.
Ichanged the cout statement at least for testing to see what was happening: std::cout << "The man traveled " << blocksTraveled << " blocks on trip number " << i << " to reach location: " << location << std::endl;
I added this after the cout statement: location = 2;. This allowed for 5 different trips.
Thank you Andy! That does certainly help, and now that you mention the "location = 2;" portion that you added, I see that I didn't reset my variable, which is part of the issue. I'll work on revising it. I appreciate the help!