Pick a number

--Originally published at Programming in C++

The Program:

 

Imagen1

For this program we use 2 more libraries besides iostream. First we use srand. This is going to give us a random number.

The problem is that it will give us the same random number every time we run it.

To change that we are going to add the library time, this is going to set the random number to the current time, so it won’t give the same number unless you run it at the same second.

We are also going to need a several loops. First we want the program to run every time we insert an incorrect number, for this we use a “while”. Then we use “if” to condition when the given number is too high or too low, and for when the number is actually correct.

I consult K Hong blog. Which explains in a better way the random part. I also found this image about it:

Dilbert_random