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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Random Numbers

Random Numbers

Scheduled Pinned Locked Moved C / C++ / MFC
c++javascripthelptutorialquestion
9 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.
  • S Offline
    S Offline
    Steven M Hunt
    wrote on last edited by
    #1

    I am trying to create one of those tip dialog boxes for my program and I want C++ to show random tips. Can I get C++ to "pick" a number in a range of numbers? I know how to do that in JavaScript but that doesn't help me much here. -- Steve

    N D 2 Replies Last reply
    0
    • S Steven M Hunt

      I am trying to create one of those tip dialog boxes for my program and I want C++ to show random tips. Can I get C++ to "pick" a number in a range of numbers? I know how to do that in JavaScript but that doesn't help me much here. -- Steve

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

      This might help a little :-

      int _tmain(int argc, _TCHAR* argv[])
      {
      //display 10 numbers between 0 and 9
      srand(time(NULL));
      for(int i=0; i<10; i++)
      {
      int r = (int)(((double)rand()/(double)RAND_MAX) * (double)10);
      printf("%d\r\n",r);
      }
      return 0;
      }

      Regards, Nish


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

      S S 2 Replies Last reply
      0
      • N Nish Nishant

        This might help a little :-

        int _tmain(int argc, _TCHAR* argv[])
        {
        //display 10 numbers between 0 and 9
        srand(time(NULL));
        for(int i=0; i<10; i++)
        {
        int r = (int)(((double)rand()/(double)RAND_MAX) * (double)10);
        printf("%d\r\n",r);
        }
        return 0;
        }

        Regards, Nish


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

        S Offline
        S Offline
        Steven M Hunt
        wrote on last edited by
        #3

        That line of code you gave me keeps returning 0 and just using the rand() function keeps returning 41. Isn't that strange? -- Steve

        P N 2 Replies Last reply
        0
        • S Steven M Hunt

          That line of code you gave me keeps returning 0 and just using the rand() function keeps returning 41. Isn't that strange? -- Steve

          P Offline
          P Offline
          Paul M Watt
          wrote on last edited by
          #4

          SteveBob wrote: just using the rand() function keeps returning 41. Isn't that strange? It will always return the same sequence of numbers because rand() is a psuedo-random number generator. The way you can get around this problem is by seeding the number generator the first time you use it with a value that always changes, like the current time. Try something like this:

          srand(time());


          Build a man a fire, and he will be warm for a day
          Light a man on fire, and he will be warm for the rest of his life!

          N S 2 Replies Last reply
          0
          • S Steven M Hunt

            That line of code you gave me keeps returning 0 and just using the rand() function keeps returning 41. Isn't that strange? -- Steve

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

            SteveBob wrote: That line of code you gave me keeps returning 0 Weird. I got outputs like :- 6 0 9 8 9 2 0 5 8 7 and 6 8 8 7 1 2 0 8 9 8 Regards, Nish p.s. please copy/paste the code exactly and try again!


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

            1 Reply Last reply
            0
            • P Paul M Watt

              SteveBob wrote: just using the rand() function keeps returning 41. Isn't that strange? It will always return the same sequence of numbers because rand() is a psuedo-random number generator. The way you can get around this problem is by seeding the number generator the first time you use it with a value that always changes, like the current time. Try something like this:

              srand(time());


              Build a man a fire, and he will be warm for a day
              Light a man on fire, and he will be warm for the rest of his life!

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

              Paul Watt wrote: Try something like this: srand(time()); The code snippet I gave him already does that! I think he didn't copy/paste it properly. Nish


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

              1 Reply Last reply
              0
              • P Paul M Watt

                SteveBob wrote: just using the rand() function keeps returning 41. Isn't that strange? It will always return the same sequence of numbers because rand() is a psuedo-random number generator. The way you can get around this problem is by seeding the number generator the first time you use it with a value that always changes, like the current time. Try something like this:

                srand(time());


                Build a man a fire, and he will be warm for a day
                Light a man on fire, and he will be warm for the rest of his life!

                S Offline
                S Offline
                Steven M Hunt
                wrote on last edited by
                #7

                Thanks! That fixed the problem! -- Steve

                1 Reply Last reply
                0
                • S Steven M Hunt

                  I am trying to create one of those tip dialog boxes for my program and I want C++ to show random tips. Can I get C++ to "pick" a number in a range of numbers? I know how to do that in JavaScript but that doesn't help me much here. -- Steve

                  D Offline
                  D Offline
                  devvvy
                  wrote on last edited by
                  #8

                  i've taken so much from the forum and i just finished a module: it's time for me to do one good deed =) call srand, then rand. Quote MSDN: /* RAND.C: This program seeds the random-number generator * with the time, then displays 10 random integers. */ #include #include #include void main( void ) { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); } norm

                  1 Reply Last reply
                  0
                  • N Nish Nishant

                    This might help a little :-

                    int _tmain(int argc, _TCHAR* argv[])
                    {
                    //display 10 numbers between 0 and 9
                    srand(time(NULL));
                    for(int i=0; i<10; i++)
                    {
                    int r = (int)(((double)rand()/(double)RAND_MAX) * (double)10);
                    printf("%d\r\n",r);
                    }
                    return 0;
                    }

                    Regards, Nish


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

                    S Offline
                    S Offline
                    Sean_Yang
                    wrote on last edited by
                    #9

                    If I want to get 1, 2 and 3 with EXACT 1/3 chance, how can I do it? int choice; srand(time(NULL)); while(1) { choice= rand() MOD 3; ..... } is there any better method? Thanks Sean

                    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