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. Randomic functions ?

Randomic functions ?

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

    I would like to use a function that returns a integer value between two values. I met the rand() function, but I need a value in a range. []'s Cristiano.

    N 1 Reply Last reply
    0
    • C Cris

      I would like to use a function that returns a integer value between two values. I met the rand() function, but I need a value in a range. []'s Cristiano.

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      (rand()/RAND_MAX)*5 for [between 0-5] (rand()/RAND_MAX)*n for [between 0-n]


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      L C C 3 Replies Last reply
      0
      • N Nish Nishant

        (rand()/RAND_MAX)*5 for [between 0-5] (rand()/RAND_MAX)*n for [between 0-n]


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Or easier, rand () % n If you want a range between A and B then just shift everything along: int value = a + (rand () % (b - a));

        1 Reply Last reply
        0
        • N Nish Nishant

          (rand()/RAND_MAX)*5 for [between 0-5] (rand()/RAND_MAX)*n for [between 0-n]


          Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

          C Offline
          C Offline
          Cris
          wrote on last edited by
          #4

          This not work corretly. The returned value is zero. []'s

          L 1 Reply Last reply
          0
          • C Cris

            This not work corretly. The returned value is zero. []'s

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The following will print 100 random values between 120 and 320 int getRange (int low, int high) { return low + (rand () % (high - low)); } int main (int argc, char *argv[]) { srand (time (NULL)); for (int x = 0; x < 100; x++) printf ("Random Value %d = %d\n", x, getRange (120, 320)); return 0; }

            1 Reply Last reply
            0
            • N Nish Nishant

              (rand()/RAND_MAX)*5 for [between 0-5] (rand()/RAND_MAX)*n for [between 0-n]


              Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

              C Offline
              C Offline
              CoY0te
              wrote on last edited by
              #6

              Almost... The problem is: int rand(void); so it should be: rand()*n/RAND_MAX or ((double)rand()/RAND_MAX)*n Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.

              J 1 Reply Last reply
              0
              • C CoY0te

                Almost... The problem is: int rand(void); so it should be: rand()*n/RAND_MAX or ((double)rand()/RAND_MAX)*n Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.

                J Offline
                J Offline
                Joaquin M Lopez Munoz
                wrote on last edited by
                #7

                This is not correct. The expression yields a value between 0 and n, but the probability of n is miniscule (1/(RAND_MAX+1), to be precise), which is not the probability for the other values. The following return a random value between 0 and n with uniform distribution:

                int randn(int n)
                {
                int r;
                while((r=rand())==RAND_MAX);
                return ((double)r/RAND_MAX)*(n+1);
                }

                Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                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