subtraction and division using srand

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sttip
    New Member
    • May 2007
    • 2

    subtraction and division using srand

    Hi,

    I'm a student taking a beginner's c++ course. I am trying to write a program that performes simple math operations on random numbers. I am using the
    srand(int(time( 0))); function to generate the random numbers. I have two variables that I'm using to perform the operations upon. I was wondering if there was a way to tell one of the variables to be less than the other so that negative answers are not generated during subtraction and division. Here is a snippet:

    Code:
    srand(int(time(0)));
       vara = 1 + rand() % (1000 - 1 + 1);
       varb = 1 + rand() % (1000 - 1 + 1);
       answera = 0;
       cout << "What is " << vara << '-' << varb << '?' << endl; 
       cin >> response;
       answera = vara - varb;
    Don't know how far off I am....like I said just a beginner. Any advice would be helpful.
    Last edited by AdrianH; May 8 '07, 02:29 PM. Reason: Please use [code][/code] tags for readability.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by sttip
    I was wondering if there was a way to tell one of the variables to be less than the other so that negative answers are not generated during subtraction and division.
    I would just compare the numbers before doing the subtraction and swap them if the second one turns out to be larger. (New code in bold)
    Code:
    srand(int(time(0)));
       vara = 1 + rand() % (1000 - 1 + 1);
       varb = 1 + rand() % (1000 - 1 + 1);
       answera = 0;
       
    [B]    if( varb > vara ) // if b is larger swap a and b
        {
            int tmp = vara;
            vara = varb;
            varb = tmp;    
        }[/B]
    
       cout << "What is " << vara << '-' << varb << '?' << endl; 
       cin >> response;
       answera = vara - varb;

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by sttip
      Hi,

      I'm a student taking a beginner's c++ course. I am trying to write a program that performes simple math operations on random numbers. I am using the
      srand(int(time( 0))); function to generate the random numbers. I have two variables that I'm using to perform the operations upon. I was wondering if there was a way to tell one of the variables to be less than the other so that negative answers are not generated during subtraction and division. Here is a snippet:

      Code:
      srand(int(time(0)));
         vara = 1 + rand() % (1000 - 1 + 1);
         varb = 1 + rand() % (1000 - 1 + 1);
         answera = 0;
         cout << "What is " << vara << '-' << varb << '?' << endl; 
         cin >> response;
         answera = vara - varb;
      Don't know how far off I am....like I said just a beginner. Any advice would be helpful.
      The formula you gave: "vara = 1 + rand() % (1000 - 1 + 1);" You don't need the - 1 + 1 as they cancel each other out.

      So what you have is vara and varb being assigned a value between 1 and 1000.

      If you just want one to be always greater than the other, just check after you generate the numbers and swap them if they are reverse of what you want.


      Adrian

      Comment

      • sttip
        New Member
        • May 2007
        • 2

        #4
        Originally posted by AdrianH
        The formula you gave: "vara = 1 + rand() % (1000 - 1 + 1);" You don't need the - 1 + 1 as they cancel each other out.

        So what you have is vara and varb being assigned a value between 1 and 1000.

        If you just want one to be always greater than the other, just check after you generate the numbers and swap them if they are reverse of what you want.


        Adrian
        Thank you for swap suggestion. I wrote the srand statement from reading directions in textbook. Maybe I misunderstood. Thanks for help.

        Comment

        Working...