how to generate random numbers when sum up has fixed number

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.
Last edited on
Generating 3 random numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <random>

int main()
{
    std::random_device generator;
    std::uniform_int_distribution<int> distribution(0, 9);

    for (unsigned short int n = 0; n != 3; ++n)
    {
        std::cout << distribution(generator) << '\n';
    }

    return 0;
}

random_device may not work with some compilers. If so, use mt19937.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <random>
#include <functional>
#include <chrono>

int main()
{
    std::mt19937::result_type seed =
    std::chrono::high_resolution_clock::now().time_since_epoch().count();

    auto get_rand =
    std::bind(std::uniform_int_distribution<int>(0, 9), std::mt19937(seed));

    unsigned short int rand_num = get_rand();

    std::cout << rand_num << '\n';

    return 0;
}
Last edited on
I think I prefer integralfx's method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace 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] << " ";
}
Last edited on
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).

generated percentages (integralfix)
------------------
0 - 22.8636 %
1 - 16.2061 %
2 - 12.8609 %
3 - 10.6239 %
4 -  8.9627 %
5 -  7.6260 %
6 -  6.5315 %
7 -  5.5772 %
8 -  4.7427 %
9 -  4.0054 %

generated percentages (lastchance)
------------------
0 - 18.1787 %
1 - 16.3649 %
2 - 14.5506 %
3 - 12.7195 %
4 - 10.9182 %
5 -  9.0880 %
6 -  7.2691 %
7 -  5.4602 %
8 -  3.6303 %
9 -  1.8205 %

expected percentages
------------------
0 - 18.1818 %
1 - 16.3636 %
2 - 14.5455 %
3 - 12.7273 %
4 - 10.9091 %
5 -  9.0909 %
6 -  7.2727 %
7 -  5.4545 %
8 -  3.6364 %
9 -  1.8182 %

all possible 3-tuples
------------------
 1. 0 0 9
 2. 0 1 8
 3. 0 2 7
 4. 0 3 6
 5. 0 4 5
 6. 0 5 4
 7. 0 6 3
 8. 0 7 2
 9. 0 8 1
10. 0 9 0
11. 1 0 8
12. 1 1 7
13. 1 2 6
14. 1 3 5
15. 1 4 4
16. 1 5 3
17. 1 6 2
18. 1 7 1
19. 1 8 0
20. 2 0 7
21. 2 1 6
22. 2 2 5
23. 2 3 4
24. 2 4 3
25. 2 5 2
26. 2 6 1
27. 2 7 0
28. 3 0 6
29. 3 1 5
30. 3 2 4
31. 3 3 3
32. 3 4 2
33. 3 5 1
34. 3 6 0
35. 4 0 5
36. 4 1 4
37. 4 2 3
38. 4 3 2
39. 4 4 1
40. 4 5 0
41. 5 0 4
42. 5 1 3
43. 5 2 2
44. 5 3 1
45. 5 4 0
46. 6 0 3
47. 6 1 2
48. 6 2 1
49. 6 3 0
50. 7 0 2
51. 7 1 1
52. 7 2 0
53. 8 0 1
54. 8 1 0
55. 9 0 0

http://coliru.stacked-crooked.com/a/e72a1ec4b45457f6
Last edited on
"Random" doesn't mean the same as "independent".

"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.

wahaha11's statement..

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.
I think we had better ask wahaha11 to clarify.
Topic archived. No new replies allowed.