dimanche 21 avril 2019

How do I generate random numbers in a range?

I'm creating a program for school that works as follows: user thinks of a number and computer tries to guess it. computer generates a random number, asks user if number is higher or lower, then if the number is higher it takes that number and sets that as the minimum value for the random number range(ex. generates a 47, then will only generate numbers between 47-100). if the number is lower it takes the random number that was generated and makes that the minimum value.

My problem is with the random number generator and the range. I can generate the random number by seeding it at the start { srand(time(NULL));} then generate a number { rand_num = rand()%100; } generates a number between 1-100. but I can't figure out how to generate a number in a range (example 47-89). thought it was {rand_num = rand()%89 + 47}, but this doesn't seem to work.

Github: https://gist.github.com/EthanA2020/e121b27ab80fa6e2d32df1396e62b632

{

srand(time(NULL));
do {
rand_num=rand()%47+89;
cout << rand_num << endl;
cout << "enter: "; cin >> userquitval;
}while (userquitval!=7);

}




Aucun commentaire:

Enregistrer un commentaire