Using Rand()

Hey,

I am working on a project for class where I need to create a very basic game. The short version is I have two runners, Runner One moves forward at a random rate, Runner Two moves forward two steps at a time. I get the program to run, Runner One is definitely running random steps, but it doesn't progress forward it's literally just random. The steps are supposed to range from 1-10 per turn. Does anyone have any suggestions for what I can do? Runner Two progresses forward fine.

Thank 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

 #include <iostream>

using namespace std;

int main(){
    
//Main Loop
    double stepOne = 1, stepTwo = 0;
    
    cout << "Runner One is a step" "\n";
    cout << stepOne;
    cout << "Runner Two is at step " "\n";
    cout << stepTwo;
    
    while (stepOne <= 100){
        stepOne++;
    while (stepTwo <=100){
        stepTwo++;
    
    
// runner one running random amount of steps till 100
    cout << "RunnerOne is at Step " "\n";
        stepOne = rand() % 12;
        cout << stepOne << endl;
    

// runner two Running 2 steps at a time till 100
    cout << "RunnerTwo is at Step " "\n";
        stepTwo = stepTwo + 2;
        cout << stepTwo << endl;
    
 
}
    }
}
Last edited on
stepOne += rand() % 12;
or, equivalently,
stepOne = stepOne + rand() % 12;

Also, you should
#include <cstdlib>

I'm sure you know this, but on average your runningOne will clearly be faster (average step will be 5).

1
2
3
4
5
6
7
8
9
10
while (stepOne <= 100){
    stepOne++;
    while (stepTwo <=100){
        stepTwo++;
        
        // ...        
        stepOne += rand() % 12;
        stepTwo += stepTwo + 2;
    }
}

Are you sure this is what you want? A while loop within a while loop?

Perhaps you are actually trying to something like,
1
2
3
4
5
6
7
while (stepOne <= 100 && stepTwo <= 100){

    // ...        
    stepOne += rand() % 12;
    stepTwo += 2;

}
Last edited on
your code, properly indented
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
#include <iostream>
using namespace std;

int main() {
  // Main Loop
  double stepOne = 1, stepTwo = 0; //¿why different starts?

  cout << "Runner One is a step"
          "\n";
  cout << stepOne;
  cout << "Runner Two is at step "
          "\n ";
  cout << stepTwo;

  while (stepOne <= 100) {
    stepOne++; //¡¿?!
    while (stepTwo <= 100) { //¿why nested?
      stepTwo++; //¡¿?!

      // runner one running random amount of steps till 100
      cout << "RunnerOne is at Step "
              "\n";
      stepOne = rand() % 12; //0 to 11
      cout << stepOne << endl;

      // runner two Running 2 steps at a time till 100
      cout << "RunnerTwo is at Step "
              "\n";
      stepTwo = stepTwo + 2;
      cout << stepTwo << endl;
    }
  }
}
Yeah sorry, I was messing around with stuff at the time and forgot to change it back before I posted. This is what I have currently have.

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

//Main Loop
double stepOne = 0, stepTwo = 0;

cout << "Runner One is a step" "\n";
cout << stepOne;
cout << "Runner Two is at step " "\n";
cout << stepTwo;

while (stepOne <= 100){
stepOne++;
while (stepTwo <=100){
stepTwo++;


// Runner One running random amount of steps forward till 100
cout << "RunnerOne is at Step " "\n";
stepOne = rand() % 10;
cout << stepOne << endl;


// Runner Two running 2 steps at a time till 100
cout << "RunnerTwo is at Step " "\n";
stepTwo = stepTwo + 2;
cout << stepTwo << endl;


}
}
}


Gando I tried what you posted by adding a += to the function but it didn't stop at 100. I did not put that in the original post though. The first runner to 100 wins. But it did at least progress Runner One forward. Thank You

ne555 I have different starts because like I said earlier I was messing around with a few things and forgot to change it back before posting. I am not sure what you mean why nested though, can you explain? Thank You.
This is what is being asked of me...

Random Race Two runners are competing in a race. Runner 1 runs at a constant pace, while runner 2's pace is random. The runners start at the same place and the first to cross the finish line wins. If they both cross the finish line in the same turn, consider the race a tie.

Rules of the game:
The race is 100 steps long. The runners start at step 0.

Runner 1 is a steady runner, going two steps at a time.
Runner 2 is a random runner, and occasionally may get turned around and run the wrong way.

Runner 2’s pace and direction is determined by the roll of a 12-sided die with numbers 1 through 12 on it.

If the number rolled is even, runner 2 goes that many steps forward. If the number rolled is odd, runner 2 goes that many steps backward. However, runner 2 will never cross backward over the start line and go to a negative step number.

If the step number becomes negative, set it back to 0.

All moves are determined by the program randomly. There is no user input.
What happens in a turn:
Runner 1 moves 2 steps forward. Runner 2 moves a random number of steps forward or backward.
> I am not sure what you mean why nested though
this is what you have
1
2
3
4
5
6
7
	while(stepOne <= 100) { //outer loop (A)
		stepOne++;
		while(stepTwo <= 100) { //inner loop (B)
			stepTwo++;
			//...
		}
	}
loop B is inside loop A, it is nested
you check the condition in `A', increment `stepOne' (¿why?), then iterate on loop B
loop B finish, then you iterate on loop A


> the first to cross the finish line wins.
1
2
3
4
while(runnerA <= 100 or runnerB <= 100){
	//...
}
//determine the winner 



> Runner 2’s pace and direction is determined by the roll of a 12-sided die with numbers 1 through 12 on it.
as a suggestion, don't try to do it all at once
you have three things here:
1. the position of the runner
2. the roll of the dice
3. the movement amount
make them three separate variables
1
2
3
int dice = rand()%12;
int movement = /*rules for decide movement*/;
position += movement;
Topic archived. No new replies allowed.