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. Random Numbers

Random Numbers

Scheduled Pinned Locked Moved C / C++ / MFC
questionlounge
10 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
    Renjith Ramachandran
    wrote on last edited by
    #1

    i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

    K E N D 4 Replies Last reply
    0
    • R Renjith Ramachandran

      i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #2

      Try

         // Seed the random-number generator with current time so that
         // the numbers will be different every time we run.
      
          srand( (unsigned)time( NULL ) ); // include for this
      
      for( int i = 0;  i < 20; i++ )
      {
      	int n = int (50 \*  ( (double) rand() / RAND\_MAX ));
      	cout << n << "\\n";
      }
      

      Typical output: 4 48 10 16 27 12 10 27 45 39 24 15 7 3 12 22 24 25 19 28 Kevin

      R 1 Reply Last reply
      0
      • R Renjith Ramachandran

        i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

        E Offline
        E Offline
        eli15021979
        wrote on last edited by
        #3

        Hi, try this: srand( (unsigned)time( NULL )); int num = rand()%50; Regards, Eli

        R K 2 Replies Last reply
        0
        • E eli15021979

          Hi, try this: srand( (unsigned)time( NULL )); int num = rand()%50; Regards, Eli

          R Offline
          R Offline
          Renjith Ramachandran
          wrote on last edited by
          #4

          yeah great...thankyou Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

          1 Reply Last reply
          0
          • E eli15021979

            Hi, try this: srand( (unsigned)time( NULL )); int num = rand()%50; Regards, Eli

            K Offline
            K Offline
            Kevin McFarlane
            wrote on last edited by
            #5

            Of course! Much simpler than mine! Kevin

            1 Reply Last reply
            0
            • R Renjith Ramachandran

              i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

              N Offline
              N Offline
              nadzzz
              wrote on last edited by
              #6

              use the modulo (%) //0 to 49 int nNumber = (rand() % 50); //1 to 50 int nNumber between1and50 = 1 + (rand() % 50);

              1 Reply Last reply
              0
              • K Kevin McFarlane

                Try

                   // Seed the random-number generator with current time so that
                   // the numbers will be different every time we run.
                
                    srand( (unsigned)time( NULL ) ); // include for this
                
                for( int i = 0;  i < 20; i++ )
                {
                	int n = int (50 \*  ( (double) rand() / RAND\_MAX ));
                	cout << n << "\\n";
                }
                

                Typical output: 4 48 10 16 27 12 10 27 45 39 24 15 7 3 12 22 24 25 19 28 Kevin

                R Offline
                R Offline
                Ryan Binns
                wrote on last edited by
                #7

                Kevin McFarlane wrote: int n = int (50 * ( (double) rand() / RAND_MAX )); Aaaah, finally someone else who knows the best way to use rand(). Most people just use the % operator, but statistically, using your approach leads to more randomness, as the rand() algorithm has the property that the higher-order bits are more random than the lower order bits. Or did you just guess? ;) Either way, well done :)

                Ryan

                "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                K 1 Reply Last reply
                0
                • R Renjith Ramachandran

                  i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.

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

                  renjith_sree wrote: i want to generate some random numbers, Unfortunately this is impossible using a computer algorithm. The best you can hope for is a pseudo-random number. renjith_sree wrote: but with in a limit ( i.e i want to generate the random numbers with in 50)... Is 50 the upper limit or the lower limit?


                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                  1 Reply Last reply
                  0
                  • R Ryan Binns

                    Kevin McFarlane wrote: int n = int (50 * ( (double) rand() / RAND_MAX )); Aaaah, finally someone else who knows the best way to use rand(). Most people just use the % operator, but statistically, using your approach leads to more randomness, as the rand() algorithm has the property that the higher-order bits are more random than the lower order bits. Or did you just guess? ;) Either way, well done :)

                    Ryan

                    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #9

                    I just guessed. In fact I'd thought that eli15021979's % method was slicker! But you learn something new every day.:) Kevin

                    R 1 Reply Last reply
                    0
                    • K Kevin McFarlane

                      I just guessed. In fact I'd thought that eli15021979's % method was slicker! But you learn something new every day.:) Kevin

                      R Offline
                      R Offline
                      Ryan Binns
                      wrote on last edited by
                      #10

                      Kevin McFarlane wrote: In fact I'd thought that eli15021979's % method was slicker! It is :) And it will be good enough most of the time, but technically you can do better without having to change an awful lot ;). In fact, if you use the method you originally posted, modifying it to work with floating-point numbers is relatively trivial - a lot easier than converting one using the modulo operator, since the modulo operator doesn't work for floating-point numbers, well not in C++ at least...

                      Ryan

                      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                      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