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. How do i create a random number?

How do i create a random number?

Scheduled Pinned Locked Moved C / C++ / MFC
questionlounge
6 Posts 6 Posters 0 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.
  • R Offline
    R Offline
    Redeemer dk
    wrote on last edited by
    #1

    Is there a function for retreiving a random integer lying in the range from one integer to another? I'm using Win32... THanks in advance.

    C D R A 4 Replies Last reply
    0
    • R Redeemer dk

      Is there a function for retreiving a random integer lying in the range from one integer to another? I'm using Win32... THanks in advance.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      see rand() and srand(..) srand(-1); int v = (rand() % (low + high)) + low; To vote with no response is to follow the way of the coward.

      D 1 Reply Last reply
      0
      • R Redeemer dk

        Is there a function for retreiving a random integer lying in the range from one integer to another? I'm using Win32... THanks in advance.

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

        No, but you can get a pseudo-random number with srand() and rand().

        1 Reply Last reply
        0
        • R Redeemer dk

          Is there a function for retreiving a random integer lying in the range from one integer to another? I'm using Win32... THanks in advance.

          R Offline
          R Offline
          RedZenBird
          wrote on last edited by
          #4

          See article by: Stephen K. Park and Keith W. Miller. RANDOM NUMBER GENERATORS: GOOD ONES ARE HARD TO FIND. Communications of the ACM, New York, NY.,October 1988 p.1192 Just trying to keep the forces of entropy at bay

          1 Reply Last reply
          0
          • C Chris Losinger

            see rand() and srand(..) srand(-1); int v = (rand() % (low + high)) + low; To vote with no response is to follow the way of the coward.

            D Offline
            D Offline
            Dean Goodman
            wrote on last edited by
            #5

            Check out this page -- shows a better way to get your random numbers. When you use modular arithmetic, you focus on the low-order bits that aren't as "random" as the high-order ones. The page shows many examples of how to get a random number (int or floating point) in a given range. --Dean

            1 Reply Last reply
            0
            • R Redeemer dk

              Is there a function for retreiving a random integer lying in the range from one integer to another? I'm using Win32... THanks in advance.

              A Offline
              A Offline
              Anders Molin
              wrote on last edited by
              #6

              #define GetRandom(min, max) ((rand() % (int)(((max) + 1) - (min))) + (min)) //get a random number between min and max Remember something like srand(GetTickCount()); first... - Anders Money talks, but all mine ever says is "Goodbye!"

              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