How to add random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by namcintosh
    So do I type the variable int result after the unsigned seed??
    You don't need an 'unsigned seed' variable either. Please don't try to program
    by guessing and randomly (sic) copying and pasting snippets of code.

    kind regards,

    Jos

    Comment

    • namcintosh
      New Member
      • Apr 2007
      • 67

      #17
      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

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

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

      int main()
      {
      unsigned seed;


      cout << "Enter a seed value: " ;
      cin >> seed;
      srand (seed);
      int result= 1+rand()%500;When I key in this line, an error pops up that says "result is assigned to a value that is never used.
      srand(time(0));
      cout << rand() <<endl;
      cout << rand() <<endl;

      getch();
      return 0;
      }


      Output Line:
      Enter a seed value: 1
      6978
      10643


      Run the program and you will see what I mean

      Comment

      • namcintosh
        New Member
        • Apr 2007
        • 67

        #18
        Originally posted by JosAH
        You don't need an 'unsigned seed' variable either. Please don't try to program
        by guessing and randomly (sic) copying and pasting snippets of code.

        kind regards,

        Jos

        I am sooo confused! I'm not really good at this. I am following my textbook's instructions, but nothing seems to be working. Maybe I ought to work on another program and come back to this one.

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

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

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

          int main()
          {
          unsigned seed;


          cout << "Enter a seed value: " ;
          cin >> seed;
          srand (seed);
          int result= 1+rand()%500;When I key in this line, an error pops up that says "result is assigned to a value that is never used.
          srand(time(0));
          cout << rand() <<endl;
          cout << rand() <<endl;

          getch();
          return 0;
          }


          Output Line:
          Enter a seed value: 1
          6978
          10643


          Run the program and you will see what I mean
          Like Jos just said you don't need a seed variable. You just need the
          "srand(time(0)) " line before genrating the random numbers.
          Also you don't need a result variable. Instead of, "cout << rand() << endl;"
          put "cout << rand()%500 << endl;".
          (

          Comment

          • namcintosh
            New Member
            • Apr 2007
            • 67

            #20
            Originally posted by ilikepython
            Like Jos just said you don't need a seed variable. You just need the
            "srand(time(0)) " line before genrating the random numbers.
            Also you don't need a result variable. Instead of, "cout << rand() << endl;"
            put "cout << rand()%500 << endl;".
            (

            Okay, I got it!! Thanks so much!!!

            I will show my completed program when I am finished.

            :-) :-) :-) :-)

            Comment

            • namcintosh
              New Member
              • Apr 2007
              • 67

              #21
              How to add random numbers

              Does anyone have any idea how to add random numbers that are generated in C++?

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #22
                Originally posted by namcintosh
                Does anyone have any idea how to add random numbers that are generated in C++?
                namcintosh, you're familiar with our Posting Guidelines, so what have you tried with this?

                Comment

                • ilikepython
                  Recognized Expert Contributor
                  • Feb 2007
                  • 844

                  #23
                  Originally posted by namcintosh
                  Does anyone have any idea how to add random numbers that are generated in C++?
                  Unless I'm mistaken by what you mean, try the '+' sign.
                  That's pretty basic stuff, you shoud check out these websites:





                  They have good tutorials for beginners.

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #24
                    namcintosh-

                    Please do not double-post your question. I have merged these threads.

                    Comment

                    • namcintosh
                      New Member
                      • Apr 2007
                      • 67

                      #25
                      Originally posted by ilikepython
                      Unless I'm mistaken by what you mean, try the '+' sign.
                      That's pretty basic stuff, you shoud check out these websites:





                      They have good tutorials for beginners.
                      well, I tried, but my input didn't come out right.

                      Here's what I mean:
                      Code:
                      int main()
                      {
                              unsigned seed;
                      
                              cout << "Enter a seed value: " ;
                              cin  >> seed;
                              srand(time(0));
                              cout << rand()%500 << endl;
                              cout << rand()%500 << endl;
                              cout << rand()%500 + rand()%500;
                              getch();
                              return 0;
                      }
                      Output:
                      Enter a seed value: 1
                      385
                      425
                      644
                      Last edited by sicarie; Apr 8 '07, 01:38 AM. Reason: Please use [code] and [/code] tags around your code.

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #26
                        Originally posted by namcintosh
                        well, I tried, but my input didn't come out right.

                        Here's what I mean:
                        Code:
                        int main()
                        {
                                unsigned seed;
                        
                                cout << "Enter a seed value: " ;
                                cin  >> seed;
                                srand(time(0));
                                cout << rand()%500 << endl;
                                cout << rand()%500 << endl;
                                cout << rand()%500 + rand()%500;
                                getch();
                                return 0;
                        }
                        Output:
                        Enter a seed value: 1
                        385
                        425
                        644
                        Please use code tags around your code - it makes everything much more readable.

                        What about that is not right? You never use your seed value, you cin it, but then set time(0) - the current system time - as the seed, not the number you cin.

                        Comment

                        • namcintosh
                          New Member
                          • Apr 2007
                          • 67

                          #27
                          Originally posted by sicarie
                          Please use code tags around your code - it makes everything much more readable.

                          What about that is not right? You never use your seed value, you cin it, but then set time(0) - the current system time - as the seed, not the number you cin.

                          I'm confused. What do u mean???

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #28
                            Originally posted by namcintosh
                            I'm confused. What do u mean???
                            About what?

                            Your variable? I think that's pretty self-explanatory. Look at the code you wrote, and look at the comment. Code tags are explained on the right-hand side of the screen when you respond, and are appreciated.

                            Other than that, you need to ask a more specific question. Your code is working, and returning values between 1 and 500...

                            Comment

                            • namcintosh
                              New Member
                              • Apr 2007
                              • 67

                              #29
                              Originally posted by sicarie
                              About what?

                              Your variable? I think that's pretty self-explanatory. Look at the code you wrote, and look at the comment. Code tags are explained on the right-hand side of the screen when you respond, and are appreciated.

                              Other than that, you need to ask a more specific question. Your code is working, and returning values between 1 and 500...

                              All I am trying to figure out how to do is sum up those two random numbers. Do I store them as variables??

                              Comment

                              • Ganon11
                                Recognized Expert Specialist
                                • Oct 2006
                                • 3651

                                #30
                                Yes, you will have to. Every time you call the rand() function, it generates a new random number. If you want to keep these numbers to do something with them, like adding, you will have to store the values of the rand() call into a variable, like this:

                                Code:
                                int num = rand();

                                Comment

                                Working...