I wanna generate three random numbers in a row. These three numbers should be within 0,9. When sum up these three numbers, they will give a fixed number, let's say 9. one possible solution is 1,3,5. please help!
Generate a pseudo-random number using rand(). (1)
Subtract (1) from 9. (2)
Generate another pseudo-random number using rand() between [0, 9 - (1)].
Subtract from (2).
Result is your third number.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int a[3];
int sum = 9;
srand( time(0) );
a[0] = rand() % ( sum + 1 ); sum -= a[0];
a[1] = rand() % ( sum + 1 ); sum -= a[1];
a[2] = sum;
for ( int i = 0; i < 3; i++ ) cout << a[i] << " ";
}
As a matter of interest, just what do you intend to do with your three random (but not independent) numbers?
"Random" thought: the method above is a very biased way of generating triplets. With it:
- the first digit has a 1/10 chance of being a 9.
- the last digit has only a 1/100 chance of being a 9.
Life for the youngest is never fair!
Here's a less biased (if considerably more expensive) way of sharing out the spoils.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int a[3];
int sum;
int target = 9;
srand( time(0) );
do
{
sum = 0;
for ( int i = 0; i < 3; i++ )
{
a[i] = rand() % ( target + 1 );
sum += a[i];
}
} while ( sum != target );
for ( int i = 0; i < 3; i++ ) cout << a[i] << " ";
}
I'm skeptical about calling these 'random' numbers though. only the first number is truly random given the sum ==9 condition, after that the conditional distribution of the second number is not independent of the first (for e.g if the first number is 8 the second can only be 0 or 1) and so on ...
Assuming that all possible 3-tuples of non-negative integers that add up to nine are equally probable, lastchance's algorithm appears to give unbiased (close to unbiased) results.
Since there are only 55 such 3-tuples, an efficient algorithm would be to generate the 55 possibilities (once) and then pick one of them at random (each time).
"Random" simply means that their outcome is not fixed, but is prescribed by some probability distribution: we use many different probability distributions, most of them non-uniform.
"Independent" would mean the conditional probabilities are the same; that won't be the case here since the sum is fixed.
It depends how they are to be generated (hence my two different codes).
In the first code the probability that the first digit is a 9 is 1/10 (the sequence having to be 9-0-0) and the probability that the last digit is a 9 is 1/100 ( the sequence having to be 0-0-9). In the second code I haven't managed to work out the probability that the first digit is a 9 (yet - I'm thinking about it!), but I know that it is the same as the probability that the last one is a 9. Perhaps I should try a Monte Carlo simulation ...!
JLBorges' and my reply obviously crossed, but he has saved me some work. With the second code the probability that the first digit is a 9 appears to be 1/55.
Thanks JLBorges!
There's a lot more to this problem than originally thought.
I wanna generate three random numbers in a row. These three numbers should be within 0,9. When sum up these three numbers, they will give a fixed number, let's say 9. one possible solution is 1,3,5. please help!
This seems to me, that he wanted 3 random numbers from 0 to 9, added all together. So number one could be 4, second number might be 6 and the last, a 9. All together they equal 19. Then he wants each 3 number combo that would equal the 19.
1 - 2 - 16
1 - 3 - 15
...
5 - 6 - 8
etc.
Not that the random 3 numbers HAVE to equal 9.
This of course, is my understanding of the given problem.