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 generate random number?

How to generate random number?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutoriallounge
5 Posts 3 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.
  • D Offline
    D Offline
    DaveE9th
    wrote on last edited by
    #1

    I want to generate a random number between a specified range of 1 to 100, how can I do this? Thanks in advance, Dave :-D

    "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

    D 1 Reply Last reply
    0
    • D DaveE9th

      I want to generate a random number between a specified range of 1 to 100, how can I do this? Thanks in advance, Dave :-D

      "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

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

      Technically, you can't generate a truly random number. You can, however, get pseudo-random numbers using rand(). For your specific range, try:

      int x = (rand() % 100) + 1;

      You may or may not also want to use srand(), which seeds the algorithm.

      D 1 Reply Last reply
      0
      • D David Crow

        Technically, you can't generate a truly random number. You can, however, get pseudo-random numbers using rand(). For your specific range, try:

        int x = (rand() % 100) + 1;

        You may or may not also want to use srand(), which seeds the algorithm.

        D Offline
        D Offline
        DaveE9th
        wrote on last edited by
        #3

        Thanks, I'll give it a try. :-D

        "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

        C 1 Reply Last reply
        0
        • D DaveE9th

          Thanks, I'll give it a try. :-D

          "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

          C Offline
          C Offline
          Chunhua Liu
          wrote on last edited by
          #4

          You'd better initialize the seed first. Use srand(time(NULL)). Otherwise you will always get the same random number sequence.

          D 1 Reply Last reply
          0
          • C Chunhua Liu

            You'd better initialize the seed first. Use srand(time(NULL)). Otherwise you will always get the same random number sequence.

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

            Which is not necessarily a bad thing, especially when trying to reproduce a problem.

            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