-1

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);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ethan A
  • 21
  • 1
  • 3
  • Could you please clarify. "Between 1 to 100" could mean any number excluding the end points. Did you mean "From 1 to 100"? I also suggest examining the code at the limits to see if it generates the ranges you expect. – doug Apr 21 '19 at 21:35
  • @doug from 1-99... – Ethan A Apr 21 '19 at 21:47
  • Then `rand_num=rand()%99+1;` produces numbers from 1-99 – doug Apr 21 '19 at 21:52
  • Possible duplicate of [Generating random integer from a range](https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range) – Nick Apr 21 '19 at 21:52
  • 4
    This question is tagged as C++. Why aren’t you using the C++ random library? – Indiana Kernick Apr 21 '19 at 21:55
  • @Kerndog73 ``, It's an older code, sir, but it checks out. – wally Apr 21 '19 at 21:58
  • @kerndog73 codeblocks doesn't recognize it – Ethan A Apr 21 '19 at 21:59
  • I think what @Kerndog73 is getting at is that it would be much better to use the newer way. If your tools don't support a modern version then it might be time to upgrade. – wally Apr 21 '19 at 22:00
  • @wally tbh my teacher showed us this way and I have no idea how to use the random library – Ethan A Apr 21 '19 at 22:08
  • 1
    I agree with @wally. The random library looks way more complicated than it actually is when you’re reading the docs. It’s what you should be using. – Indiana Kernick Apr 21 '19 at 22:22
  • Worth watching: [rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – Jesper Juhl Apr 22 '19 at 11:37

2 Answers2

5

To generate a random number in C++11 or later, use the C++ random library. This should be much preferred over the C utilities for any application.

#include <random>
#include <iostream>

int main() {
  std::random_device seed;
  std::mt19937 gen{seed()}; // seed the generator
  std::uniform_int_distribution<> dist{47, 89}; // set min and max
  int guess = dist(gen); // generate number
  std::cout << "Computer guess: " << guess << '\n';
}

Read the relevant cppreference page for more information.

starriet
  • 2,565
  • 22
  • 23
Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
1

In the modulo operator you have to use the length of the interval you want the values in.

rand_num=rand()%(90-47)+47;

As mentioned by Jesper, this introduces a bias. To understand it, think what happens if you want a number equal to 0 or 1, and the range of rand is [0, 1, 2]. With the above formula, the probability of getting a 0 would be 0.66 and the probability of getting a 1 would be 0.33).

To overcome this problem, you can use the following formula that scales the random number to a float in the [0,1] interval, and then use that to generate the number you need.

(double) uniform = (double) rand() / (double) RAND_MAX;
rand_num = (int) ((90.0 - 47.0)*uniform) + 47);
eguaio
  • 3,754
  • 1
  • 24
  • 38
  • Did you mean `rand_num=rand()%(89-47+1)+47;` which produces numbers from 47 to 89 – doug Apr 21 '19 at 21:40
  • sure, it depends if you want to include the 89 as a posibility. I edited the answer to reflect that. – eguaio Apr 21 '19 at 21:45
  • That modulo (`%`) introduces bias, since the input range is not evenly divisible by the output range. A better solution is to use `std::uniform_int_distribution`. – Jesper Juhl Apr 22 '19 at 15:38
  • @JesperJuhl that is true. The bias is really small if the range is small, but it is indeed not null. I'll add a comment on that. – eguaio Apr 22 '19 at 17:37