Generating random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 9966
    New Member
    • Sep 2007
    • 16

    Generating random numbers

    Greetings,

    This is my second post till now. Thanks for all the advice given to me for the first post.

    Now I'm having problem with generating random numbers. I know if we want to generate a random number, we can use:

    int tempNUM = rand();

    But, once I execute the program, the generated number is always similar, and I found this from the internet. It says need to add this line to make the numbers generated differently:

    srand((unsigned )time(0));

    When I add that line, my result is like hell.. Could someone please kindly enlighten me. Thank you
  • ruskalym
    New Member
    • Sep 2007
    • 65

    #2
    What kind of hell is the result you get?

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      If you want to limit the results from the random number you can use the modules operator.

      Number = rand() % 200// generates a number from 0-199

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Yeah, you're not actually generating a "random number" per se, but more a pseudo-random number. There are algorithms for getting a very dispersed range, some positive, some negative, etc.... One cool thing is that if you set the seed with the same number, you can come up with the same 'random numbers' each time to duplicate experiments.

        You probably are getting a 'random number.' Is there a specific range that you need it be between? (Ah, see Studlyami's post above for how to get it to be in a range)

        Comment

        • Jayant Vikram
          New Member
          • Oct 2007
          • 2

          #5
          Plz,

          I couldn't find any new & interesting topic of my C language project so I want a topic only .....

          Give me suggestions for the topic of my C language project ...

          NOTE :- I have knowledge of C language only so give only those topic in which C language is used......

          Send ur suggestion Quickly my friends.....

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Jayant Vikram-

            Posting your own question in a thread is called thread hijacking, and not only is it extremely rude, but it is also against the Posting Guidelines for theScripts. Please review the Guidelines and ask your own question in your own thread.

            Comment

            • 9966
              New Member
              • Sep 2007
              • 16

              #7
              Yes I understand that the range can be obtained as follows to get numbers between 1~3:

              int randNum;
              randNum = (rand()%3)+1;

              But the problem I faced right now is, let's say I've add on a cout as follows:

              cout << "Number generated: " << rand() << endl;

              Let's give an example, when I run the program for the first time, the result is:

              Number generated: 0.41

              I close the command prompt and run the program the second time, it still shows me the same result, which is the problem I'm facing now. I want the numbers to differ each time I run the program. Any advice would be appreciated. Thanks

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by 9966
                srand((unsigned )time(0));
                This didn't work? Are you getting errors? What are you getting?

                Comment

                • 9966
                  New Member
                  • Sep 2007
                  • 16

                  #9
                  No, it's not a compilation error. Well, before I add on that line, the program can run properly. But after I add on that line, the result shown on the command prompt is like an endless loop whereby the numbers are jumping non-stop.

                  Since after adding that line will make the numbers generated randomly, but I'm still setting a proper range for the numbers generated in order to get the range that I want. I can't understand why this line can suddenly make my programming not working. My programming is not really completed yet so I can't really show it here though...

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    If you can't post the snippet of code you're having trouble with, it's going to be a bit difficult to help troubleshoot it any further.

                    Comment

                    • 9966
                      New Member
                      • Sep 2007
                      • 16

                      #11
                      Or am I doing the wrong thing? without that line, rand() itself will generate numbers between 0~1 right? Because:

                      tempNUM = rand();
                      cout << "Random number generated is :" << tempNUM << endl;

                      and all I see is the same (for e.g) 0.41.


                      What will happen if I add srand((unsigned )time(0)), will the numbers being generated more than 1?

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        I'm not really sure what your implementation is - I keep getting (mostly) large values, so I'm going to point you to the documentation of rand(), and wish you the best. If you want to post code, we could probably help more, but I'm not sure what exactly you're doing in your program.

                        Comment

                        • 9966
                          New Member
                          • Sep 2007
                          • 16

                          #13
                          I'm actually solving a TSP problem whereby I need to write a function to search the possible randomly. So I need to generate random numbers to help me choose which paths should I take in case there are more than 1 path. My current program is very very long already, so I don't know which snippet should I post it here. But anyway, thanks a lot for your advice. I think I've made some mistake in vectors due to random numbers. Because I'm using a lot of

                          vector1[randNum] = ......

                          So I think I'll take a look at the documentation you have given. Thanks a lot

                          Comment

                          Working...