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 to get a random number between 0 and 1

how to get a random number between 0 and 1

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlounge
4 Posts 4 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.
  • C Offline
    C Offline
    cauvang
    wrote on last edited by
    #1

    I want to get a random number between 0 and 1.Can you help me?Thanks a lot.

    S S K 3 Replies Last reply
    0
    • C cauvang

      I want to get a random number between 0 and 1.Can you help me?Thanks a lot.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Did you try the rand()[^] function? It returns an int, but you can always divide the resulting int by pow(10, sizeof(int) + 1) to get a number between 0 and 1. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • C cauvang

        I want to get a random number between 0 and 1.Can you help me?Thanks a lot.

        S Offline
        S Offline
        seppo lahti
        wrote on last edited by
        #3

        std::rand() / (double)RAND_MAX gives a uniformly distributed random number in range [0...1]. Following function gives a uniformly distributed value in half-open range [lo…hi). template inline T random(T lo, T hi) { return (T)(lo + (__int64)std::rand() * (hi - lo) / (RAND_MAX + 1)); } Seppo -- modified at 9:09 Monday 10th October, 2005

        1 Reply Last reply
        0
        • C cauvang

          I want to get a random number between 0 and 1.Can you help me?Thanks a lot.

          K Offline
          K Offline
          kevincwong
          wrote on last edited by
          #4

          How about 1 divid by the system generated random number? So, if the system generated number is 10 => 1/10 = 0.1 100 -> 1/100 = 0.01 & etc.

          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