Write an algorithm to generate random numbers from 1 to 12 with equal probability using a given function which generates random numbers from 1 to 6 with equal probability.

The idea is to make two separate calls to the specified function and store the result in two variables, say x and y, which would be random numbers between 1 to 6. Then we can easily establish that –

- The expression
`2*x`

returns an even random number between 2 to 12 (i.e. 2, 4, 6, 8, 10 and 12) with equal probability.

- The expression
`(y & 1)`

returns either 0 or 1 depending upon whether y is even or odd.

The idea is to use the the expression `(x * 2) – (y & 1)`

will return random numbers from 1 to 12 with equal probability. This works since

- if
`(y & 1)`

is 0, the expression returns the random even numbers 2, 4, 6, 8, 10, and 12 with equal probability.

- if
`(y & 1)`

is 1, the expression returns the random odd numbers 1, 3, 5, 7, 9, and 11 with equal probability.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <stdlib.h> #include <time.h> // Function that returns random numbers from 1 to 6 with equal probability int random() { return (rand() % 6) + 1; } // Generate random numbers between 1 to 12 with equal probability using a // function that generates random numbers from 1 to 6 with equal probability int generate() { int x = random(); int y = random(); return 2*x - (y & 1); } int main(void) { // initialize srand with distinctive value srand(time(NULL)); int freq[13]; memset (freq, 0, sizeof(freq)); for (int i = 0; i < 1000000; i++) { int val = generate(); freq[val]++; } for (int i = 1; i <= 12; i++) printf("%2d ~ %0.2f%%\n", i, freq[i]/10000.0); return 0; } |

`Output: `

1 ~ 8.33%

2 ~ 8.35%

3 ~ 8.35%

4 ~ 8.31%

5 ~ 8.32%

6 ~ 8.33%

7 ~ 8.29%

8 ~ 8.38%

9 ~ 8.35%

10 ~ 8.34%

11 ~ 8.35%

12 ~ 8.31%

Another way to generate the desired random numbers can be done using the expression

`x + (y & 1)*6`

or `x + !(y & 1)*6`

where x and y represents output of two distinct calls made to the `random()`

function.

##### How this works?

Let’s consider the `x + (y & 1)*6`

expression:

`x`

returns random numbers from 1 to 6 with equal probability.

`(y & 1)`

returns 0 or 1 depending upon whether y is even or odd.

Now if y is even, the expression reduces to x, which gives random numbers from 1 to 6 and if y is odd, the expression is reduced to `(x + 6)`

, which gives random numbers from 7 to 12.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <stdlib.h> #include <time.h> // Function that returns random numbers from 1 to 6 with equal probability int random() { return (rand() % 6) + 1; } // Generate random numbers between 1 to 12 with equal probability using a // function that generates random numbers from 1 to 6 with equal probability int generate() { int x = random(); int y = random(); return x + (y & 1)*6; } int main(void) { // initialize srand with distinctive value srand(time(NULL)); int freq[13]; memset (freq, 0, sizeof(freq)); for (int i = 0; i < 1000000; i++) { int val = generate(); freq[val]++; } for (int i = 1; i <= 12; i++) printf("%2d ~ %0.2f%%\n", i, freq[i]/10000.0); return 0; } |

`Output: `

1 ~ 8.29%

2 ~ 8.35%

3 ~ 8.34%

4 ~ 8.39%

5 ~ 8.29%

6 ~ 8.32%

7 ~ 8.31%

8 ~ 8.32%

9 ~ 8.36%

10 ~ 8.34%

11 ~ 8.32%

12 ~ 8.37%

**Author: **Aditya Goel

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply