How to add random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • namcintosh
    New Member
    • Apr 2007
    • 67

    How to add random numbers

    I am trying to do a math tutor program which generates two random numbers from 1 to 50 and add them up. I have no clue as to where to start.

    Any input is appreciated.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at the rand() and srand() functions; both are declared in stdlib.h.
    Read all about them here .

    kind regards,

    Jos

    Comment

    • namcintosh
      New Member
      • Apr 2007
      • 67

      #3
      Originally posted by JosAH
      Have a look at the rand() and srand() functions; both are declared in stdlib.h.
      Read all about them here .

      kind regards,

      Jos
      Okay, thanks. I will let you have the honor of seeing my program when it is completed. :-)

      Comment

      • namcintosh
        New Member
        • Apr 2007
        • 67

        #4
        Originally posted by JosAH
        Have a look at the rand() and srand() functions; both are declared in stdlib.h.
        Read all about them here .

        kind regards,

        Jos
        Well, I tried, and this is what happened:

        Program
        #include <iostream>
        #include <cstdlib>
        #include <conio>
        using namespace std;

        int main()
        {
        unsigned seed;
        int num;

        num = 1 + rand() %500;

        srand (seed);

        cout << rand() <<endl;
        cout << rand() <<endl;
        cout << rand() <<endl;

        getch();
        return 0;
        }

        Output:
        130
        10982
        1090
        Each time I run this program, I get the same three random numbers. what's the deal

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by namcintosh
          Well, I tried, and this is what happened:

          Program
          #include <iostream>
          #include <cstdlib>
          #include <conio>
          using namespace std;

          int main()
          {
          unsigned seed;
          int num;

          num = 1 + rand() %500;

          srand (seed);

          cout << rand() <<endl;
          cout << rand() <<endl;
          cout << rand() <<endl;

          getch();
          return 0;
          }

          Output:
          130
          10982
          1090
          Each time I run this program, I get the same three random numbers. what's the deal
          I think it is because you didn't set seed to anything. It is usually a good idea to set seed to the system time so it is different everytime you run the program.
          Code:
          srand(unsigned time(0))
          also i think you have to include a time header file.

          Comment

          • namcintosh
            New Member
            • Apr 2007
            • 67

            #6
            Originally posted by ilikepython
            I think it is because you didn't set seed to anything. It is usually a good idea to set seed to the system time so it is different everytime you run the program.
            Code:
            srand(unsigned time(0))
            also i think you have to include a time header file.

            Okay, so this is the program
            #include <iostream>
            #include <cstdlib>
            #include <time>
            #include <conio>
            using namespace std;

            int main()
            {
            unsigned seed, time;
            int num;

            num = 1 + rand() %500; When I use this line, I get an error that says num is assigned to a value that is never used.

            srand (seed);
            srand (time);
            cout << rand() <<endl;
            cout << rand() <<endl;

            getch();
            return 0;
            }

            And this is my output:
            10673
            25698

            See the problem?? it's not generating the two random numbers from the range of 1-500. When I ran it again, it got the same random numbers. What to do??

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              You never use the value of 'num' anywhere in your code hence the warning.
              For the identical output for each run: carefully reread the previous person's
              reply again: the code uses a *function* time(), not a *variable* that is accidentally
              named 'time'.

              kind regards,

              Jos

              Comment

              • namcintosh
                New Member
                • Apr 2007
                • 67

                #8
                Originally posted by JosAH
                You never use the value of 'num' anywhere in your code hence the warning.
                For the identical output for each run: carefully reread the previous person's
                reply again: the code uses a *function* time(), not a *variable* that is accidentally
                named 'time'.

                kind regards,

                Jos

                Well, I tried that, and this is what happened:

                #include <iostream>
                #include <cstdlib>
                #include <time>
                #include <conio>
                using namespace std;

                int main()
                {
                unsigned seed, time;

                time = 1 + rand() %500;

                srand (seed);
                srand (time);
                cout << rand() <<endl;
                cout << rand() <<endl;

                getch();
                return 0;
                }

                Output Line
                4862
                12702
                Each time I run this, I get the same numbers. I don't want that. I want a different set of random numbers each time, in between the range of 1 to 500.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  This always produces the same value:

                  time = 1 + rand() %500;

                  When uses with srand(), it's the same seed so you get the same numbers from rand().

                  Why not key in the seed???

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by namcintosh
                    Each time I run this, I get the same numbers. I don't want that. I want a different set of random numbers each time, in between the range of 1 to 500.
                    You didn't read ilikepython's reply carefully enough. There is no need for a
                    local variable named time. It's the time() function that does it:
                    Code:
                    srand(time(0));
                    That seeds the random number generator to different values each time you run
                    your program because the value returned by the time() function will be different
                    each run.

                    kind regards,

                    Jos

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      You also have to put the srand() call before you generate any random numbers.

                      Comment

                      • namcintosh
                        New Member
                        • Apr 2007
                        • 67

                        #12
                        Originally posted by weaknessforcats
                        This always produces the same value:

                        time = 1 + rand() %500;

                        When uses with srand(), it's the same seed so you get the same numbers from rand().

                        Why not key in the seed???
                        So how do I get the random number that output on my screen to range from 1 to 500??? That is what I'm really trying to do.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by namcintosh
                          So how do I get the random number that output on my screen to range from 1 to 500??? That is what I'm really trying to do.
                          The following is statistically a bit incorrect, but for starters you could do:
                          Code:
                          int result= 1+rand()%500;
                          The variable 'result' will have a value in the interval [1, 500].

                          kind regards,

                          Jos

                          Comment

                          • namcintosh
                            New Member
                            • Apr 2007
                            • 67

                            #14
                            Originally posted by Ganon11
                            You also have to put the srand() call before you generate any random numbers.
                            Is this how I declare my range to be from 1 to 50??

                            srand(time(1+ra nd() % 50));

                            What is the zero for in the parenthesis???

                            Comment

                            • namcintosh
                              New Member
                              • Apr 2007
                              • 67

                              #15
                              Originally posted by JosAH
                              The following is statistically a bit incorrect, but for starters you could do:
                              Code:
                              int result= 1+rand()%500;
                              The variable 'result' will have a value in the interval [1, 500].

                              kind regards,

                              Jos
                              So do I type the variable int result after the unsigned seed??

                              Comment

                              Working...