# Random number issue

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-14-2008
swgh
Random number issue
Hey guys

I am writing a very simple game of war and my random output seem's a bit bizzare. Code will explain it better.

Code:

```// computer's turn to choose its leigons         unsigned short compFoot = 1 + rand() % 50;         unsigned short compFly = 1 + rand() % 50;         unsigned short compHse = 1 + rand() % 50;         unsigned short compAgt = 1 + rand() % 50;```
Which is fine - I always get a random unit number for each leigon when I run the program.

Now, the problem comes when I do this:

Code:

`short defeatedCmpFoot = 1 + rand() % compFoot;`
To choose how many of that unit have been defeated, I either get to more than the chosen number in the first peice of code I posted or I repeatedly get the same number for every single unit being defeated.

I do have

Code:

`srand((unsigned)time(0));`
At the start - but I cannot figure out what is going wrong with the random number choice. Everything else works fine and the game runs through ok but I am obviously getting invalid results at the end due to the random number.

Can sombody give me any pointers? Any help appreiciated!
• 11-14-2008
matsp
I don't see anything directly wrong in what you have posted. Perhaps there is code elsewhere that is causing a problem?

--
Mats
• 11-14-2008
laserlight
Perhaps you should post the smallest and simplest compilable program that demonstrates the problem.
• 11-14-2008
swgh
Ok - this is the whole code for one battle.

Passing the player chosen values by reference would not be the reason would it?

Code:

```// computer's turn to choose its leigons         unsigned short compFoot = 1 + rand() &#37; 50;         unsigned short compFly = 1 + rand() % 50;         unsigned short compHse = 1 + rand() % 50;         unsigned short compAgt = 1 + rand() % 50;         std::cout << "\n\nYou have chosen:\n\n"                           << "Footman: " << rplyFoot                           << "\nAirman: " << rplyFly                           << "\nHorseman: " << rplyHse                           << "\nAgents: " << rplyAgt                           << "\n\nComputer has chosen:\n\n"                           << "Footman: " << compFoot                           << "\nAirman: " << compFly                           << "\nHorseman: " << compHse                           << "\nAgents: " << compAgt << "\n\n"                           << "Press enter see what happened!\n\n";                 // ignore last key stroke         std::cin.ignore();         std::cin.get();                 // simulate a very simple battle         short playerWin = 0,                   computerWin = 0,                   draw = 0;         std::string theWinner = "";         short defeatedPlyFoot = 1 + rand() % rplyFoot;         short defeatedCmpFoot = 1 + rand() % compFoot;         if ( defeatedPlyFoot > defeatedCmpFoot )         {                 computerWin++;                 theWinner = "\nThe computer won the leigon!\n";         }         if ( defeatedCmpFoot > defeatedPlyFoot )         {                 playerWin++;                 theWinner = "\nYou won the leigon!\n";         }         else         {                 draw++;                 theWinner = "\nBoth leigon's were lost on battle - a draw!\n";         }         std::cout << "\nTotal Player footman defeated: " << defeatedPlyFoot                           << "\nTotal Computer footman defeated: " << defeatedCmpFoot                           << "\n" << theWinner << std::endl;```
• 11-14-2008
Codeplug
>> I do have ... At the start
Start of the program or function?

gg
• 11-14-2008
swgh
Start of the program - Main only has the srand() function and one function which calls the main game loop.
• 11-14-2008
matsp
I still can't see anything wrong in your code (it looks like it could do with a bit of splitting into several functions, but that's a different matter).

I think you need to follow Laserlights suggestion and come up with a complete, compilable, sample that can be run standalone).

--
Mats
• 11-14-2008
swgh
This is the best I can do as simular thing - and still I get the same sort of results:

Code:

```#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main ( void ) {     srand((unsigned)time(0));         int numberOfApples = 1 + rand() &#37; 50;     int chosenApples = 1 + rand() % numberOfApples;         cout << "I chose: " << chosenApples << endl;     cout << "Peter chose: " << chosenApples << endl;         cin.get();         return 0; }```
Would posting the full code give more of an idea?
• 11-14-2008
laserlight
Quote:

Originally Posted by swgh
This is the best I can do as simular thing - and still I get the same sort of results:

What sort of results are you expecting, and what sort of results did you get?
• 11-14-2008
matsp
And how long beteween each run. Remember that you get SIMILAR random numbers for SIMILAR time values. You may want to hash the seed, see http://www.eternallyconfuzzled.com

--
Mats
• 11-14-2008
tabstop
Changing the first number to numberOfApples, I got (just now) 10 and 5, 17 and 13, and 24 and 21. Is that not (the sort of thing) that you get?
• 11-14-2008
swgh
For the main program I wrote this the sort of thing I would expect to see if it was working:

Code:

```Total Player footman defeated: 15 Total Computer footman defeated: 10 The Computer won the leigon! Total Player airman defeated: 1 Total Computer airman defeated: 18 You  won the leigon!```
But I am getting:

Code:

```Total Player footman defeated: 2 Total Computer footman defeated: 70 You won the leigon! Total Player airman defeated: 2 Total Computer airman defeated: 59 You  won the leigon!```
The numbers of the "Player" defeated are ALWAYS the same on every leigon when I have asked it to be random.

And the computer defeated is always outside of the range of what hte computer picked.

And thanks matsp I will take a look at the link.
• 11-14-2008
laserlight
Looking back at the "whole code for one battle", it is clear that we do not actually see the part where the player values are generated, so perhaps the error lies there, especially since "numbers of the "Player" defeated are ALWAYS the same on every leigon".
• 11-14-2008
tabstop
Regardless of the numbers being similar, 1 + rand %50 shouldn't give you 70. I don't see how int vs. unsigned short vs. short would cause that sort of thing either. This is one of those cases where I think that the code we're seeing is somehow not the code that is running.
• 11-14-2008
swgh
Thanks everyone so far for the help and suggestions.

Laserlight, this is how the player values for each leigon is produced:

Code:

```const int MAX_SIZE = 50;                 unsigned short totalFoot = 0,                                       totalFly = 0,                                           totalHse = 0,                                           totalAgt = 0;                 while (( totalFoot <= 0 ) || ( totalFoot > MAX_SIZE ))                 {                         std::cout << "\nHow many footman will you send: ";                         std::cin >> totalFoot;                         if ( totalFoot > MAX_SIZE )                         {                                 std::cout << "\nMax is 50 per unit!\n\n";                         }                 }                 rplyFoot = totalFoot; // the rplyFoot is passed to the battle function```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last