So i'm trying to code a game for context. I'd like to be able to get 2 random numbers(a, b) that will add up to less then number n.
Here's my initial approach.
I get a random number between 0 and n.
Lets says n = 100 in an example. And my first random number is 78.
so i would take the remainder, in this case 22 and find a random number between 0 and 22. And then it would spit out some random number.
I can't seem to think of an approach where the second number gets as much "randomness" opportunity as the first number, due to the range being reduced.
What are some more approaches to getting 2 random numbers that must be less then sum n?
This method selects two random nonnegative integers $a$ and $b$ so that $a+b< n$. This method is perfect in the sense that every possible ordered pair $(a,b)$ is equally likely. This means that $a$ and $b$ will have the exact same degree of randomness.
Choose an integer $x$ randomly in the range $1$ to $n+1$.
Choose an integer $y$ randomly in the range $1$ to $n$.
Let $a=\min(x,y)-1$, and let $b=\max(x,y)-\min(x,y)-1$.