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. A continuous random number generator?

A continuous random number generator?

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlounge
6 Posts 5 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.
  • A Offline
    A Offline
    avimitrani
    wrote on last edited by
    #1

    rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.

    C W D 3 Replies Last reply
    0
    • A avimitrani

      rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      avimitrani wrote:

      is there no escape from calling rand() twice and then multiplying the results?

      That's not really random, nor does it give a wide enough range. Try calling it twice and shifting the first result to take the bits above the second result.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • A avimitrani

        rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.

        W Offline
        W Offline
        wheelerbarry
        wrote on last edited by
        #3

        You could generate a random number between 0 and 1, then multiply that by the bigger number you want to use, e.g.

        flaot fRand = rand() / (float)RAND_MAX; // Generate a random number between 0 and 1.

        int iValue = fRand * 10000000; // Generate a random number between 0 and 10000000 (where 10000000 if your new max value).

        C 1 Reply Last reply
        0
        • W wheelerbarry

          You could generate a random number between 0 and 1, then multiply that by the bigger number you want to use, e.g.

          flaot fRand = rand() / (float)RAND_MAX; // Generate a random number between 0 and 1.

          int iValue = fRand * 10000000; // Generate a random number between 0 and 10000000 (where 10000000 if your new max value).

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

          That won't work: some values might have no chances at all to be picked.


          Cédric Moonen Software developer
          Charting control [Updated - v1.1]

          A 1 Reply Last reply
          0
          • C Cedric Moonen

            That won't work: some values might have no chances at all to be picked.


            Cédric Moonen Software developer
            Charting control [Updated - v1.1]

            A Offline
            A Offline
            avimitrani
            wrote on last edited by
            #5

            Exactly!

            1 Reply Last reply
            0
            • A avimitrani

              rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.

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

              avimitrani wrote:

              What if I want to generate random integers in a wider range?

              You'll need to roll your own. Something like this.


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              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