Thread: Random number issue

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    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!
    Double Helix STL

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything directly wrong in what you have posted. Perhaps there is code elsewhere that is causing a problem?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you should post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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() % 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;
    Double Helix STL

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I do have ... At the start
    Start of the program or function?

    gg

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Start of the program - Main only has the srand() function and one function which calls the main game loop.
    Double Helix STL

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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?
    Double Helix STL

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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.
    Double Helix STL

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Issues
    By sansterre in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2009, 05:59 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM