--Originally published at Ken's Disciple 01
Picture by Stokpic
As you may imagine, this blog will be about WSQ03, in which we were asked to write a program that randomly picked a number from 1 to 100, ask the user to try to guess it, give him “too high” and “to low” hints and also showing the amount of guesses.
This Program is kind of challenging because in order to do this you have to use a “new” function that allows the program to choose the random number, this page may help you to learn the new functions you’ll need.
You don’t have to worry about this program, it’s actually easy once you have everything you need. All right so let’s get started:
- Starting this program is just doing the same as usual, inside your main, you’ll have to declare your variables, you’ll need 3; one for the random number that the program will choose, one for the guessed number that the user will give and the last one is the one that will tell you the amount of times you tried to guess the number.
- The next thing you’ll have to know is the function srand(time(0)); (for more info you can visit this page.
srand()
gives the random function a new seed, a starting point.time(0)
with these you’re guaranteed your seed will be the same only once, unless you start your program multiple times within the same second. - then you’ll use your rand function and tell the program from which numbers you want to pick a random one, here are some examples:
v1 = rand() % 100; // v1 in the range 0 to 99 v2 = rand() % 100 + 1; // v2 in the range 1 to 100
As you can see the %”…” gives you with how many numbers you want to
Continue reading "How to go: 0 to 100, real quick."