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. C++ Random Question

C++ Random Question

Scheduled Pinned Locked Moved C / C++ / MFC
c++game-devhelpquestionlounge
8 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.
  • B Offline
    B Offline
    bobski2200
    wrote on last edited by
    #1

    :)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!

    J G R C 4 Replies Last reply
    0
    • B bobski2200

      :)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      Put the line random = rand(); in the do while block and better add this to the start of the main srand(time(NULL)); this way the seed is randomly generated too (you wont start with the same population over and over again) Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      1 Reply Last reply
      0
      • B bobski2200

        :)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!

        G Offline
        G Offline
        Graham Bradshaw
        wrote on last edited by
        #3

        You need to call srand() first, otherwise rand() will always return the same number. A line like srand((unsigned)time(NULL)); before the call to rand() should do the trick.

        1 Reply Last reply
        0
        • B bobski2200

          :)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          To elaborate a bit more on the previous response, it is recommend that you first seed the random number generator. If you wish a consistent pattern of values then always seed with the same value. If not then you can use this : srand( time(NULL) % RAND_MAX ); to obtain values between 0 and 100 then you can do this : random = rand() % 101; The modulus operator returns the remainder of the division operation of the two numbers. This has the effect of clamping the return value between 0 and one less than the dividend. __________________________________________ a two cent stamp short of going postal.

          1 Reply Last reply
          0
          • B bobski2200

            :)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!

            C Offline
            C Offline
            Cohen
            wrote on last edited by
            #5

            rand() returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers. So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this: srand(static_cast<unsigned>(time(0))); Make this function call before you use rand() for the first time. If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand(). random = 1 + (rand() % 100); rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99. Hope this helps, Cohen

            B C 2 Replies Last reply
            0
            • C Cohen

              rand() returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers. So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this: srand(static_cast<unsigned>(time(0))); Make this function call before you use rand() for the first time. If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand(). random = 1 + (rand() % 100); rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99. Hope this helps, Cohen

              B Offline
              B Offline
              bobski2200
              wrote on last edited by
              #6

              Thank you for the tips guys i will try them out i just have to figure a way fo getting it to give the user 8 trys max. Thanks for not talking to clever for me lol :) This Better ******* Work!

              D 1 Reply Last reply
              0
              • C Cohen

                rand() returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers. So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this: srand(static_cast<unsigned>(time(0))); Make this function call before you use rand() for the first time. If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand(). random = 1 + (rand() % 100); rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99. Hope this helps, Cohen

                C Offline
                C Offline
                Cohen
                wrote on last edited by
                #7

                Graham - Slow Rick - Slower Cohen - Slowest :-D

                1 Reply Last reply
                0
                • B bobski2200

                  Thank you for the tips guys i will try them out i just have to figure a way fo getting it to give the user 8 trys max. Thanks for not talking to clever for me lol :) This Better ******* Work!

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

                  bobski2200 wrote: i just have to figure a way fo getting it to give the user 8 trys max. Change

                  while(guess!=random);

                  to

                  while(guess!=random && num_tries < 8);

                  Start num_tries at 0 and increment it after each guess.


                  "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