Thread: Random things are fine, just one problem with them.

  1. #1
    DarkSFK
    Guest

    Random things are fine, just one problem with them.

    I have a problem with the number game I started a few weeks ago. I stopped programming for a while and started back up. Anyway, my problem is I can make a random number, but the number generates once, and only once, not even after you shut down the application and start back up, or run it through my compiler. Here's the code:

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main()
    {
    	//initializing variables
    	int guess;
    	int loopCount=20;
    	int rand;
    	
    	//random number input
    	rand=random()%10001;
    	
    	//rules of the game
    	cout<<"This is a guessing game. You will have 20 tries to guess a random number\nbetween 1 and 10,000. If your guess is incorrect, a message will display\ntelling you if the number that is to be guessed is higher or lower.\n";
    		
    	
    	for(;loopCount>0;loopCount=loopCount--)
    	{
    		//entering a guess
    		cout<<"Enter a guess: ";
    		cin>>guess;
    		
    			if(guess==rand)
    			{
    				cout<<"That is correct! Press any key to exit.";
    				getch();
    				break;
    			}	
    			else
    			{
    				if(guess>rand)
    			 	{
    			 		cout<<"That is incorrect. The number is lower. Only "<<loopCount<<" guesses remaining.";
    			 	}
    			 	else
    			 	{
    				 	cout<<"That is incorrect. The number is higher. Only "<<loopCount<<" guesses remaining.";
    			 	}
    			}
    	}
    	return 0;
    }

  2. #2
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    You need to seed the random number generator. Use srand() to seed the generator with the current time. If you do a search, there've been a couple posts that have 'more random' number generators than the standard random.
    "Logic is the art of going wrong with confidence."
    Morris Kline

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
      //initializing variables
      int guess;
      int loopCount=20;
      int r;
      
      //random number input
      srand ( (unsigned)time ( 0 ) );
      r = rand() % 10001;
      
      //rules of the game
      cout<<"This is a guessing game. You will have 20 tries to guess a random"
        " number\nbetween 1 and 10,000. If your guess is incorrect, a message "
        "will display\ntelling you if the number that is to be guessed is higher"
        " or lower.\n";
    	
      for(;loopCount>0;loopCount=loopCount--)
      {
        //entering a guess
        cout<<"Enter a guess: ";
        cin>>guess;
        
        if(guess==r)
        {
          cout<<"That is correct! Press any key to exit.";
          getch();
          break;
        }	
        else
        {
          if(guess>r)
          {
            cout<<"That is incorrect. The number is lower.\n"
              "Only "<<loopCount<<" guesses remaining.\n";
          }
          else
          {
            cout<<"That is incorrect. The number is higher.\n"
              "Only "<<loopCount<<" guesses remaining.\n";
          }
        }
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    BTW, isn't rand() the standard C random number generator? (not random)
    "Logic is the art of going wrong with confidence."
    Morris Kline

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >BTW, isn't rand() the standard C random number generator? (not random)
    Yes, random is a non-standard addition to some compilers, but rand is always there. Just as a side note, to get random numbers from 1 to 10,000 the call to rand should be like this:
    Code:
    r = rand() % 10000 + 1;
    But, as I always stress with random numbers, a better way would be to forgo the modulus operator and divide by RAND_MAX:
    http://www.eskimo.com/~scs/C-faq/q13.16.html

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    True randominity doesn't exist. Conditions evaluate, based on earlier conditions

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by Unregistered
    True randominity doesn't exist. Conditions evaluate, based on earlier conditions
    Yeah but there is a way to achieve true randomness. I have some code for that. I read it in accelerated C++. The author said what you said, than provided a solution for the problem.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Troll_King
    Yeah but there is a way to achieve true randomness. I have some code for that. I read it in accelerated C++. The author said what you said, than provided a solution for the problem.
    Let's see it then.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    //note: #include <cstdlib>
    //note2: RAND_MAX is defined in <cstdlib>
    //note3: Function returns a random integer in the range [0, n)
    
    int nrand (int n)
    {
      if ( n <= 0 || n > RAND_MAX )
        throw domain_error ("Argument to nrand is out of range");
    
      const int bucket_size = RAND_MAX / n;
      int r;
    
      do {
        r = rand() / bucket_size;
      } while (r >= n);
    
      return r;
    }

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Troll_King
    Code:
    //note: #include <cstdlib>
    //note2: RAND_MAX is defined in <cstdlib>
    //note3: Function returns a random integer in the range [0, n)
    
    int nrand (int n)
    {
      if ( n <= 0 || n > RAND_MAX )
        throw domain_error ("Argument to nrand is out of range");
    
      const int bucket_size = RAND_MAX / n;
      int r;
    
      do {
        r = rand() / bucket_size;
      } while (r >= n);
    
      return r;
    }
    explain how that code is any more random than rand()? you give it the same seed, it will return the same numbers every time, no?
    hello, internet!

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    You might think that it would suffice to compute rand() % n, which is the remainder when dividing the random integer by n. In practice, this technique fails for two reasons.

    The most important reason is pragmatic: rand() really returns only pseudo-random numbers. Many C++ implementations pseudo-random number generators give remainders that aren't very random when the quotients are small integers. For example, it is not uncommon for successive results of rand() to be alternately even or odd. In that case, if n is 2, successive results of rand() % n will alternate between 0 and 1.

    There is another, more stable reason to avoid using rand() % n: If the value of n is large, and RAND_MAX is not evenly divisible by n, some remainders will appear more often than others. For example, suppose that RAND_MAX is 32767 (the smallest permissible value of RAND_MAX for any implementation) and n is 20000. In that case, tehre would be two distinct values of rand() tha wold cause rand() % n to be 10000 (namely 10000 and 30000), but only one value of rand() that would cause rand() % n to be 15000 (namely, 15000). Therefore, the naive implementation of nrand would yield 10000 as a value of nrand(20000) twice as often as it would yield 15000.

    To aviod these pitfalls, we'll use a different strategy, by dividing the range of available randomnumbers into buckets of exactly equal size. Then we compute a random number and return the number of the coorresponding bucket. Because the buckets are of equal size, some random numbers may not fall into any bucket at all. In that case, we keep asking for random numbers until we get one that fits.

    ..............................................

    forget it I don't want to type all thiis crap.Ther is wya too much. I'm not a professional typer. Get the book.

  12. #12
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Troll_King
    but they're still psuedorandom numbers.
    hello, internet!

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    TK, you're serious? I know you have shown yourself as someone with some knowledge but this is just wrong.

    randomness in a computer is not possible. In fact, Physicists might argue that true randomness in nature does not exist because actions are based on physical forces. The reason that your solution is not random is because the result is based on something in the system. Whenever the result is based on some existing conditions you are not random.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    p 135-136 Accelerated C++. It's truely random in the sense that all probabilies have equally likely outcomes.

  15. #15
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ok, TK. It's better than rand() in that it gives an even distribution but it is not more random. I think we're getting terms confused here.

    The even distribution problem is an ugly one though. that fix appears to be a good one.
    Last edited by FillYourBrain; 08-19-2002 at 08:43 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 05:49 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. random problem
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2002, 02:39 PM
  5. hows life fine i guess.Well i have a problem again
    By datainjector in forum C Programming
    Replies: 3
    Last Post: 07-02-2002, 11:48 PM