Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. rand() is not random

rand() is not random

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlounge
8 Posts 8 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    includeh10
    wrote on last edited by
    #1

    I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

    B _ C A S 7 Replies Last reply
    0
    • I includeh10

      I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

      B Offline
      B Offline
      BobInNJ
      wrote on last edited by
      #2

      I am not sure what you mean by submit. The function srand is to seed the random number generator. I have run some tests on the rand() function that comes with the Microsoft C/C++ compiler. I find it to be an excellent random number generator. Is it a perfect one? No. The function rand() is suppose to produce a random number whose value is independent of the last value it returned. Are you saying that for two different seeds you are getting the same sequence of random numbers? If this is true, then I think there is a problem. I have a feeling that I did not help you. I also believe that if I understood your problem better, I could give you a better answer. Bob

      1 Reply Last reply
      0
      • I includeh10

        I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Calling rand() 100 times does not guarantee 100 unique numbers if that is what you mean.

        «_Superman_»

        1 Reply Last reply
        0
        • I includeh10

          I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Please post some code. Which seed did you provide to srand ? If it is always the same, then your generated sequence will always be the same. Could that be the problem ?

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • I includeh10

            I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

            A Offline
            A Offline
            Arman S
            wrote on last edited by
            #5

            Without some code, I cannot say much. But I've the feeling you're calling srand and rand pair each time generating another random number (and which is the seed for srand?). Generally, srand is called before first call to rand and that is it.

            -- Arman

            1 Reply Last reply
            0
            • I includeh10

              I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              includeh10 wrote:

              any function which is real random

              No. A purely software function can only ever be pseudo-random. The srand function tells the rand function where to start in its pseudo-random sequence. You need a hardware source of entropy for a real random source.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              1 Reply Last reply
              0
              • I includeh10

                I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                includeh10 wrote:

                it sounds that rand() function is not random.

                It's always been known that rand(), and any other computer algorithm, produces pseudo-random numbers.

                includeh10 wrote:

                any function which is real random?

                No. The problem is not with rand(). The problem is with how you are using it.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                1 Reply Last reply
                0
                • I includeh10

                  I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each). I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit". but results are same always. it sounds that rand() function is not random. do you know how to solve the problem? any function which is real random? thanks

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  See CryptGenRandom[^] API.

                  includeh10 wrote:

                  any function which is real random?

                  From the docs: The data produced by this function is cryptographically random. It is far more random than the data generated by the typical random number generator such as the one shipped with your C compiler. ... With Microsoft CSPs, CryptGenRandom uses the same random number generator used by other security components. This allows numerous processes to contribute to a system-wide seed. CryptoAPI stores an intermediate random seed with every user.

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups