srand

Here is my code but I keep getting the same number back. I would really appreciate some help. My eyes are crossing.
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#include <iostream>
#include <array>
#include <string>
#include <iomanip>

using namespace std;
const int RACE_END = 50;
void moveTortoise(int *);
void moveHare(int *);
void printCurrentPositions(int *, int *);



int main()
{
int seed;
int tortoise=1;
int hare =1;
int timer = 0;

srand(time(0));
cout << "Welcome to Game World. Enjoy your Play" << endl;
cout << "Enter a number between 1 and 5\n";
cin >> seed;
(rand()%seed);
//cout << seed << endl;

//controls race
while (tortoise != RACE_END && hare != RACE_END)
{
moveTortoise(&tortoise);
moveHare(&hare);
printCurrentPositions(&tortoise, &hare);
++timer;
} //end while


if (tortoise >= hare)
cout << "\nTortoise wins!\n" << tortoise << endl;
else
cout << "\nHare wins!\n" << hare << endl;
system("pause");
return 0;
} //end main

void moveTortoise(int *turtlePtr)
{
int x = 1+rand()%5;
if (int x =1)
*turtlePtr += 4;
else if (int x=2)
*turtlePtr -= 2;
else if (int x = 3)
*turtlePtr += 3;
else if (int x = 4)
*turtlePtr += 4;
else if (int x = 5)
*turtlePtr -= 2;
else if (*turtlePtr > RACE_END)
*turtlePtr=RACE_END;
system("pause");
}//end move tortoise

void moveHare(int *harePtr)
{
int y = 1+rand()%5;
if (int y =1)
*harePtr += 2;
else if (int y = 2)
*harePtr -= 2;
else if (int y = 3)
*harePtr += 4;
else if (int y = 4)
*harePtr -= 3;
else if (int y = 5)
*harePtr += 1;

if (*harePtr <1)
*harePtr=1;
else if (*harePtr > RACE_END)
*harePtr=RACE_END;
} //end move hare


// output positions of animals
/* Write function definition for printCurrentPositions here */
void printCurrentPositions(int *bunnyPtr,int *snapperPtr)
{
if ( *bunnyPtr == *snapperPtr )
cout << setw( *bunnyPtr ) << "OUCH!!!";

else if ( *bunnyPtr < *snapperPtr )
cout << setw( *bunnyPtr ) << "H"
<< setw( *snapperPtr - *bunnyPtr ) << "T";

else
cout << setw( *snapperPtr ) << "T"
<< setw( *bunnyPtr - *snapperPtr ) << "H";

cout << "\n";

} // end function printCurrentPositions
1
2
srand( (unsigned)time(0) );
int random_number = (rand() % largest_possible_outcome);


Here's a proper implementation using the current time as the seed thus being more random then using a finite seed (although that works too). I'm pretty sure you are just forgetting to store the random number in a variable.
I am supposed to begin the random number generator with a seed input by the user and then it is supposed to continue to randomize after that. My trouble is how do I use a seed as the first input and then move to random? I am so grateful for your help.
srand takes the seed as an argument so get the seed from the user and pass the input to srand.

http://cplusplus.com/reference/clibrary/cstdlib/srand/

1
2
3
int seed;
cin >> seed;
srand(seed);
Last edited on
I am sorry. I am still confused. Do I change the moveTortoise/moveHare random number to random_number? I am still getting 101 for input of 1 through 5. Thank you so much.
Topic archived. No new replies allowed.