-
April 2nd, 2012, 04:51 AM
#1
Simulation: Tortoise & Hare race (srand question)
Hello guys,
I'm a new member here, and I'm currently trying to learn C++ by myself, I have some minor experience with programming through the C language.
I've been using a book to learn the language and there are some exercises that I am to take after each chapter, one of them requires me to simulate a race between a hare & a tortoise, I have a question regarding srand()
Here's my code:
Code:
// EX08.12
// Simulation: The Tortoise and the Hare
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void moveTortoise(int * const turtoisePosition);
void moveHare(int * const harePosition);
int main()
{
// seed rand
srand(time(0));
enum Status {HAREWINS, TORTOISEWINS, TIE, CONTINUE};
Status gameStatus = CONTINUE;
// initialize starting position for the race
int tortoise = 1;
int hare = 1;
// begin race
cout << "BANG!!!!\nAND THEY'RE OFF!!!!\n";
while (gameStatus == CONTINUE)
{
// move the contenders
moveTortoise(&tortoise);
moveHare(&hare);
// show race status
for (int line = 0; line < 70; line++)
{
if ((line == tortoise) && (line == hare))
cout << "Ouch!";
else if (line == tortoise)
cout << 'T';
else if (line == hare)
cout << 'H';
else
cout << '-';
}
cout << endl;
// determine whether one or the other reached the end line
if ((tortoise >= 70) && (hare >= 70))
{
cout << "It's a tie." << endl;
gameStatus = TIE;
}
else if (tortoise >= 70)
{
cout << "TORTOISE WINS!! YAY!!!" << endl;
gameStatus = TORTOISEWINS;
}
else if (hare >= 70)
{
cout << "Hare wins. Yuch!" << endl;
gameStatus = HAREWINS;
}
}
}
void moveTortoise(int * const tortoisePosition)
{
int step = 1 + rand() % 10;
// move tortoise
if ((step >= 1) && (step <= 5))
*tortoisePosition += 5;
else if ((step >=6) && (step <= 7))
*tortoisePosition -= 6;
else if ((step >= 8) && (step <= 10))
*tortoisePosition += 1;
if (*tortoisePosition < 1)
*tortoisePosition = 1;
}
void moveHare(int * const harePosition)
{
int step = 1 + (rand() % 10);
// move hare
if ((step >= 1) && (step <= 2));
else if ((step >= 3) && (step <= 4))
*harePosition += 9;
else if (step == 5)
*harePosition -= 12;
else if ((step >= 6) && (step <= 8))
*harePosition += 1;
else if ((step >= 9) && (step <= 10))
*harePosition -= 2;
if (*harePosition < 1)
*harePosition = 1;
}
My question is about line 15
If I place the line inside functions moveHare() and moveTortoise() the code produces repeated results, i.e. not random, but if I place it inside main() the code works correctly, what causes this?
Thank you for your help.
-
April 2nd, 2012, 02:35 PM
#2
Re: Simulation: Tortoise & Hare race (srand question)
Read about srand.
If you seed the random number generator with the same value, you get the same sequence.
And for the duration of your program, time(0) likely doesn't change.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
April 2nd, 2012, 02:43 PM
#3
Re: Simulation: Tortoise & Hare race (srand question)
Originally Posted by VladimirF
Read about srand.
If you seed the random number generator with the same value, you get the same sequence.
And for the duration of your program, time(0) likely doesn't change.
That's not the issue, it's when the program terminates, then I relaunch it again... I get maybe 3-4 repeated sequences if I place srand() inside the functions.
-
April 2nd, 2012, 03:25 PM
#4
Re: Simulation: Tortoise & Hare race (srand question)
Originally Posted by VeNiX
That's not the issue, it's when the program terminates, then I relaunch it again... I get maybe 3-4 repeated sequences if I place srand() inside the functions.
When you put the srand calls in moveTortoise/moveHare, you are reseeding the random number sequence upon each move.
So this (I'll show the tortoise function but it is the same for hare):
Code:
void moveTortoise(int * const tortoisePosition)
{
srand(time(0));
int step = 1 + rand() % 10;
// other code...
}
becomes equivalent to:
Code:
void moveTortoise(int * const tortoisePosition)
{
srand(SEED);
int step = 1 + rand() % 10;
// other code...
}
where 'SEED' is a constant - this is because your program runs in much less than one second, so time(0) will return the same value for all calls (except in very rare cases where the program execution straddles two values of the system clock)
Now as the seed is effectively constant, the value of 'step' will also be constant. And due to the mod 10, there will be only 10 possible values for 'step'. So your code is effectively:
Code:
void moveTortoise(int * const tortoisePosition)
{
int step = (Some number from 1 to 10 inclusive);
// other code...
}
This means there are only 10 possible different races. You are seeing fewer than this, because the 'if' statements in moveTortoise and moveHare map different values of 'step' onto the same behaviour.
Last edited by Peter_B; April 2nd, 2012 at 03:28 PM.
Reason: Changed 'less' to 'fewer'
-
April 2nd, 2012, 03:33 PM
#5
Re: Simulation: Tortoise & Hare race (srand question)
Much thanks Peter for the explanation, I guess that's what Vladimir was trying to tell me but I didn't follow.
-
April 2nd, 2012, 03:47 PM
#6
Re: Simulation: Tortoise & Hare race (srand question)
Originally Posted by VeNiX
Much thanks Peter for the explanation, I guess that's what Vladimir was trying to tell me but I didn't follow.
Yes, it was the same point Vladimir made, just with a little more detail
-
April 2nd, 2012, 04:07 PM
#7
Re: Simulation: Tortoise & Hare race (srand question)
Originally Posted by VeNiX
That's not the issue, it's when the program terminates, then I relaunch it again... I get maybe 3-4 repeated sequences if I place srand() inside the functions.
As an experiment you can declare a global variable like say,
unsigned seed = 0;
Then instead of
srand(time(0));
you call
srand(seed++);
in the functions.
Now it appears to be working and in a sense it does. What happens is that you start a new random sequence (the random sequence associated with the current value of the seed variable) before any call to rand() and rand() will always return the first number of that just started sequence.
So now you get a sequence of numbers that are seemingly random, but are they? Not necessarily because you're using the random number generator in a way it wasn't designed for. In order not to abuse the random number generator and risk a low-quality sequence, seed it once at the beginning of the program and then draw numbers from that one sequence throughout the whole of the program.
Last edited by nuzzle; April 2nd, 2012 at 04:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|