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. Rand()

Rand()

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutoriallounge
6 Posts 2 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.
  • S Offline
    S Offline
    SilverShalkin
    wrote on last edited by
    #1

    ok... hi all :) ive bean kindof relient on Kilowatt. "sorry kilowatt" now my question... i want to random a number betwean 1-10. i looked up rand()... automatically i thought that you could just put the number that you want to be randomized in the ().... like for example rand(10)... that didnt work. there is srand also... why wont the rand(10) work... and, how do i work rand()? Thanks all! ~SilverShalkin :rose:

    N 1 Reply Last reply
    0
    • S SilverShalkin

      ok... hi all :) ive bean kindof relient on Kilowatt. "sorry kilowatt" now my question... i want to random a number betwean 1-10. i looked up rand()... automatically i thought that you could just put the number that you want to be randomized in the ().... like for example rand(10)... that didnt work. there is srand also... why wont the rand(10) work... and, how do i work rand()? Thanks all! ~SilverShalkin :rose:

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

      Do a rand()%10 to generate a number between 0 and 9 Nish


      Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

      S 1 Reply Last reply
      0
      • N Nish Nishant

        Do a rand()%10 to generate a number between 0 and 9 Nish


        Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

        S Offline
        S Offline
        SilverShalkin
        wrote on last edited by
        #3

        i tried that.... cout << rand()%10 << endl; this repeats 1.. Thanks ~SilverShalkin :rose:

        N 1 Reply Last reply
        0
        • S SilverShalkin

          i tried that.... cout << rand()%10 << endl; this repeats 1.. Thanks ~SilverShalkin :rose:

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

          Yes it will, because you havent srand'd first :- srand((unsigned)time(null)); Nish


          Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

          S 1 Reply Last reply
          0
          • N Nish Nishant

            Yes it will, because you havent srand'd first :- srand((unsigned)time(null)); Nish


            Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

            S Offline
            S Offline
            SilverShalkin
            wrote on last edited by
            #5

            ok... give me an example from head to toe, about required headers... srand, RAND_MAX, and rand(). because ive tried everything, and its still doesnt work. ~SilverShalkin :rose: ps... #include i think thats the header :)

            N 1 Reply Last reply
            0
            • S SilverShalkin

              ok... give me an example from head to toe, about required headers... srand, RAND_MAX, and rand(). because ive tried everything, and its still doesnt work. ~SilverShalkin :rose: ps... #include i think thats the header :)

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

              #include "stdafx.h"
              #include <stdlib.h>
              #include <time.h>

              int _tmain(int argc, _TCHAR* argv[])
              {
              srand((unsigned)time(NULL));
              for(int i=0;i<10;i++)
              printf("%d\n",rand()%10);
              return 0;
              }

              My output :-

              D:\Projects\test\Debug>test.exe
              6
              5
              2
              0
              8
              0
              3
              1
              5
              0


              Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

              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