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
5 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
    Cedric Moonen
    wrote on last edited by
    #1

    Hello ! I would like to produce random numbers between 0 and 15. I tried to use srand and rand. So I used a similar code: #define RAND_MAX 15 //Outside of the function srand(1); int Random = rand(); But the numbers are greater than 15 :confused: How can I do that ? Thanks

    M M D 3 Replies Last reply
    0
    • C Cedric Moonen

      Hello ! I would like to produce random numbers between 0 and 15. I tried to use srand and rand. So I used a similar code: #define RAND_MAX 15 //Outside of the function srand(1); int Random = rand(); But the numbers are greater than 15 :confused: How can I do that ? Thanks

      M Offline
      M Offline
      Mike Beckerleg
      wrote on last edited by
      #2

      Just #defining your own RAND_MAX is no good. The rand() method uses the RAND_MAX that was defined when the library it was in was compiled. To get it into a particular range try something like: int Random = rand()*(15/RAND_MAX); The other thing to note is that if you always seed the random number generator with the same number (1 in your example), it will always generate the same sequence of pseudorandom numbers. Try something like: srand( (unsigned)time( NULL ) ); This seends the generator with a value based on the current time so it should give you different sequences. Mike

      C 1 Reply Last reply
      0
      • M Mike Beckerleg

        Just #defining your own RAND_MAX is no good. The rand() method uses the RAND_MAX that was defined when the library it was in was compiled. To get it into a particular range try something like: int Random = rand()*(15/RAND_MAX); The other thing to note is that if you always seed the random number generator with the same number (1 in your example), it will always generate the same sequence of pseudorandom numbers. Try something like: srand( (unsigned)time( NULL ) ); This seends the generator with a value based on the current time so it should give you different sequences. Mike

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

        Thanks ;)

        1 Reply Last reply
        0
        • C Cedric Moonen

          Hello ! I would like to produce random numbers between 0 and 15. I tried to use srand and rand. So I used a similar code: #define RAND_MAX 15 //Outside of the function srand(1); int Random = rand(); But the numbers are greater than 15 :confused: How can I do that ? Thanks

          M Offline
          M Offline
          Martijn van Kleef
          wrote on last edited by
          #4

          To produce random numbers from 0 to some predefined limit it would probably be easiest to do: #define RAND_MAX_LIMIT 15 // Set 15 as limit for randomly generated values srand((unsigned)time(NULL)); // Seed random number generator with current time int Random = rand() % (RAND_MAX_LIMIT + 1); // Only generate numbers between 0 and your predefined limit

          1 Reply Last reply
          0
          • C Cedric Moonen

            Hello ! I would like to produce random numbers between 0 and 15. I tried to use srand and rand. So I used a similar code: #define RAND_MAX 15 //Outside of the function srand(1); int Random = rand(); But the numbers are greater than 15 :confused: How can I do that ? Thanks

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

            cedric moonen wrote: I would like to produce random numbers between 0 and 15. Try rand() % 16.


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            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