-2

So the generator ask "what is 40+20" user enters 60 so its valid, if the generator prints out 80+80 the answer would be aver 160 but its not suppose to be over 100, how would I cap the the random numbers so the sums wont be over 100?

  • 2
    Are you able to get this working without the over 100 limit? If so, please provide the code. – George Feb 10 '21 at 01:26
  • You can only limit the single values to under 100. The problem is that you are selecting two values, *at random*, that must be less than 100. I could pick 3 and 99 within the array or 3 and 25. The first pair are not valid, however the 2nd pair are (and could be using the same slot holding the value 3. You'll need to present more details about your requirements. – Thomas Matthews Feb 10 '21 at 02:23

2 Answers2

1

If you need actual uniform distribution (like a fair roll dice), you can first roll the sum and then roll one of the sum ingredients:

std::random_device rd; 
std::mt19937 gen(rd()); 
std::uniform_int_distribution<> dist1(0, 100);

int sum = dist1(gen);

std::uniform_int_distribution<> dist2(0, 100 - sum);

int a = dist2(gen);
int b = sum - a;

A naive approach would be to generage both ingredients of the sum using RNG, but that would have a bias towards higher end of the range (more likely to generate sum in range 50-100 than range 0-50). This may actually be a better solution, if your program is to e.g. test user's ability to do summation up to 100 - you'd want to test them with greater numbers rather than something they can calculate using their fingers. But you could also get that benefit by setting lower range of the sum to something higher than 0.

std::random_device rd; 
std::mt19937 gen(rd()); 
std::uniform_int_distribution<> dist1(0, 100);

int a = dist1(gen);

std::uniform_int_distribution<> dist2(0, 100 - a);

int b = dist2(gen);

This little program presents the difference between both approaches. It calculates 100000 sums from each one and generates histograms to show how much sums fall into each range. It is quite bad, because 100 gets its own range in histograms, but the point should get through. In naive approach, rolling two numbers such that their sum is equal to number in range 90-100 will be much more likely than rolling two numbers which will sum to a number less than 10.


You tagged your question with , which means you should prefer to use standard library <random> over rand() (because rand() is bad). Here's version with rand() if you wish so:

int sum = rand() % 100;
int a = rand() % (100 - sum);
int b = sum - a;

Or the naive version:

int a = rand() % 100;
int b = rand() % (100 - a);
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 1
    This has the disadvantage that it tends to give answers in the higher end of 0-100 – Yakk - Adam Nevraumont Feb 10 '21 at 01:33
  • @Yakk-AdamNevraumont True. I didn't take that into account, but it seems like a reasonable bonus for one use case, so I added corrected solution as alternative. – Yksisarvinen Feb 10 '21 at 01:41
  • has to use rand(), basically a 3rd grade quiz, user enters how many questions they want but the sum of those questions cant be over 100, have everything else just need to figure out to cap the sums so i can get the code going – Omar Tapia Feb 10 '21 at 02:05
1

What do you mean by "random"?

The number 5 is a random number. I just chose it by rolling a 6 sided die. Your sum can be 5+5=10 always.

You probably mean "uniformly random". But uniform over what?

If you want the answers to be uniform from 0 to 100, first pick the answer, then pick one of the two values from 0 to the answer.

The downside is that 0 as a sum is just as common as 100, and there is exactly one set of numbers from 0 to 100 that add to 0, and 100 pairs of numbers that add to 100.

If you want each possible equation to have equal chance, an easy way is to generate two random numbers from 0 to 100, then take the highest. This is your answer. Now, pick a random number from 0 to that sum; that is the first value. The second value is the sum minus that first value.

Another approach that is simpler is to just choose a random number from 0 to 100. Then pick a second number from 0 to (100-first number). This distribution has an uneven chance for each possible equation to occur: 0+0 has a 1 in 10000 chance of occurring (0.01%), and 100+0 has a 1.01% chance of occurring, almost 100 times more likely.

But if you don't care much about the distribution, it is fine.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524