Thread: Generating random numbers

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    Generating random numbers

    I have a program that generates random numbers. After the random number is generated, the program asks if you want to generate another random number. However, if you generate another random number, it is always the same as the first random number. How can I fix this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By generating another random number.

    For more help, please post the code. If the code is too long, simplify it to the smallest and simplest (compilable) program that demonstrates the problem, then post that.
    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

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    Code:
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
        int i;
        int count;
        int j;
        srand(time(NULL)); 
        int r = (rand()%250)+50;
    
    
         printf("+_+_+_+_+_Guess My Weight_+_+_+_+_+\n\n Enter your weight as an integer between 50-200:\n");
    
            scanf("%i", &i);
                if(i<50 || i>250)
                {
                    printf("Invalid input!\n");
                }
    
                if(i>=50 || i<=250)
                {
                    if(i>=r-10 && i<=r+10)
                    {
                        printf("I guessed %i, which is within 10 pounds of your weight! Sorry, no teddy bear this time.\n", r);
                    }
                    else
                    {
                    printf("I guessed %i, which is not even close to your weight.\n\n You win a teddy bear!!\n\n", r);
                    count++;
    
                    }
                }
        printf("Do you want me to try again? (Enter: 1 for Yes, 0 for No)");
    
        scanf("%i", &j);
        
    
        
        while(j == 1)
        {
        
            printf("Enter your weight as an integer between 50-200:\n");
    
            scanf("%i", &i);
    
                if(i<50 || i>250)
                {
                    printf("Invalid input!\n");
                }
                if(i>=50 || i<=250)
                {
                    if(i>=r-10 && i<=r+10)
                    {
                        printf("I guessed %i, which is within 10 pounds of your weight! Sorry, no teddy bear this time.\n", r);
                    }
                    else
                    {
                        printf("I guessed %i, which is not even close to your weight.\n\n You win a teddy bear!!\n\n", r);
                        count++;
    
                    }
                }
        printf("Do you want me to try again? (Enter: 1 for Yes, 0 for No)\n");
    
        scanf("%i", &j);
                
        }
    
        printf("You won %i teddy bears!\n", i);
        
    return 0;    
    }
    Last edited by titaniumnuke; 10-19-2014 at 01:31 PM.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You are not generating another random number.You should have code to regenerate the random number in your while loop :-)

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    Thanks for your response. I added "srand();" to the while loop, but nothing changed. What code do i enter to the loop?

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    What do you think srand() does? It "seeds" the random number generator. In almost all cases (I do not know of an exception), it should only be called once in a program. rand() returns the generated random numbers. It should be called once for each psuedo random number which you want generated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating Random Numbers
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2009, 07:01 AM
  2. Generating random numbers...
    By naspek in forum C Programming
    Replies: 7
    Last Post: 07-29-2009, 02:00 PM
  3. Need help generating random numbers
    By com64 in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 11:15 PM
  4. Generating Random Numbers
    By pizzapie in forum C++ Programming
    Replies: 17
    Last Post: 09-23-2004, 02:46 AM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM