Thread: rand() giving the same value in while loop

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    1

    rand() giving the same value in while loop

    hello
    How can i make the rand function to generate a random number every time the loop is repeated ?

    this code is doing: generating examples for subtraction (eg 15-4 =) into 20,examples are generated until the user presses 1.
    The user enters the results of the examples.
    The program prints for each result whether it is correct or incorrect.

    (sorry for my low english)

    Code:
    #include <stdio.h>
    int main(void)
    {
      int res,range,end,resC;
      srand(time(0));  
      while(end != 1)
      {   
      int num1,num2;
      while(num1<num2)
        {            
          num1 = rand() % 20;
          num2 = rand() % 20;      
        }
     
       printf("%d - %d = ",num1,num2);     
       scanf("%d",&res);
       resC = num1 - num2;
       
       if(res != resC)printf("the result is incorrect\n");
       if(res == resC)printf("the result is correct\n");
              
       printf("to exit, press 1 and to continue press another number\n");
       scanf("%d",&end);
    }
    system("pause");
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    You're using num1 and num2 before initialising them. I suggest initialising them to the return value of rand() % 20.
    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
    May 2012
    Posts
    456
    Quote Originally Posted by laserlight View Post
    You're using num1 and num2 before initialising them. I suggest initialising them to the return value of rand() % 20.
    Yes, you need a do .. while rather than a while loop here.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop giving me a hard time once again
    By Snowflake in forum C Programming
    Replies: 2
    Last Post: 11-27-2014, 01:33 AM
  2. while loop giving me a hard time
    By Snowflake in forum C Programming
    Replies: 8
    Last Post: 11-18-2014, 08:08 AM
  3. Replies: 2
    Last Post: 10-26-2011, 01:21 AM
  4. rand() function giving same number repeatedly
    By Adam_H in forum C Programming
    Replies: 2
    Last Post: 11-15-2009, 09:43 AM

Tags for this Thread