# Generate integer from 1 to 7 with equal probability

Given a function foo() that returns integers from 1 to 5 with equal probability, write a function that returns integers from 1 to 7 with equal probability using foo() only. Minimize the number of calls to foo() method. Also, use of any other library function is not allowed and no floating point arithmetic allowed.

Solution :
We know foo() returns integers from 1 to 5. How we can ensure that integers from 1 to 7 occur with equal probability?
If we somehow generate integers from 1 to a-multiple-of-7 (like 7, 14, 21, …) with equal probability, we can use modulo division by 7 followed by adding 1 to get the numbers from 1 to 7 with equal probability.

We can generate from 1 to 21 with equal probability using the following expression.

` 5*foo() + foo() -5 `

Let us see how above expression can be used.
1. For each value of first foo(), there can be 5 possible combinations for values of second foo(). So, there are total 25 combinations possible.
2. The range of values returned by the above equation is 1 to 25, each integer occurring exactly once.
3. If the value of the equation comes out to be less than 22, return modulo division by 7 followed by adding 1. Else, again call the method recursively. The probability of returning each integer thus becomes 1/7.

The below program shows that the expression returns each integer from 1 to 25 exactly once.

## C++

 `#include ` ` `  `int` `main() ` `{ ` `    ``int` `first, second; ` `    ``for` `( first=1; first<=5; ++first ) ` `        ``for` `( second=1; second<=5; ++second ) ` `            ``printf` `(``"%d \n"``, 5*first + second - 5); ` `    ``return` `0; ` `} `

## Java

 `// Java code to demonstrate  ` `// expression returns each integer ` `// from 1 to 25 exactly once ` ` `  `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `first, second; ` `        ``for` `( first=``1``; first<=``5``; ++first ) ` `            ``for` `( second=``1``; second<=``5``; ++second ) ` `            ``System.out.printf (``"%d \n"``, ``5``*first + second - ``5``); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// Smitha Dinesh Semwal `

## Python3

 `# Python3 code to demonstrate  ` `# expression returns each integer ` `# from 1 to 25 exactly once ` ` `  `if` `name ``=``=` `'__main__'``: ` `     `  `    ``for` `first ``in` `range``(``1``, ``6``): ` `        ``for` `second ``in` `range``(``1``, ``6``): ` `                ``print``(``5` `*` `first ``+` `second ``-` `5``) ` ` `  `# This code is contributed by Smitha Dinesh Semwal. `

## C#

 `// C# code to demonstrate expression returns  ` `// each integer from 1 to 25 exactly once ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `first, second; ` ` `  `        ``for` `( first = 1; first <= 5; ++first ) ` `            ``for` `( second = 1; second <= 5; ++second ) ` `                ``Console.WriteLine ( 5*first + second - 5); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output :

```1
2
.
.
24
25```

The below program depicts how we can use foo() to return 1 to 7 with equal probability.

## C++

 `// C++ program to Generate integer from  ` `// 1 to 5 with equal probability ` `#include ` ` `  `// given method that returns 1 to 5 with equal probability ` `int` `foo()  ` `{ ` `    ``// some code here ` `} ` ` `  `int` `my_rand() ``// returns 1 to 7 with equal probability ` `{ ` `    ``int` `i; ` `    ``i = 5*foo() + foo() - 5; ` `    ``if` `(i < 22) ` `        ``return` `i%7 + 1; ` `    ``return` `my_rand(); ` `} ` `// Driver code ` `int` `main() ` `{ ` `    ``printf` `(``"%d "``, my_rand()); ` `    ``return` `0; ` `} `

## Java

 `// Java program to Generate integer from  ` `// 1 to 5 with equal probability ` `class` `GfG  ` `{ ` ` `  `// given method that returns 1 to 5 with equal probability  ` `static` `int` `foo()  ` `{  ` `    ``// some code here  ` `    ``return` `0``; ` `}  ` ` `  `// returns 1 to 7 with equal probability ` `public` `static` `int` `my_rand()   ` `{  ` `    ``int` `i;  ` `    ``i = ``5``*foo() + foo() - ``5``;  ` `    ``if` `(i < ``22``)  ` `        ``return` `i%``7` `+ ``1``;  ` `    ``return` `my_rand();  ` `}  ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args) {  ` ` `  `    ``System.out.println(my_rand());  ` `} ` `}  `

## Python3

 `# Python3 program to Generate integer  ` `# from 1 to 5 with equal probability ` `# given method that returns 1 to 5  ` `# with equal probability ` `def` `foo(): ` ` `  `    ``# some code here ` `    ``return` `0``; ` ` `  `# returns 1 to 7 with equal probability ` `def` `my_rand():  ` `    ``i ``=` `0``; ` `    ``i ``=` `(``5` `*` `foo()) ``+` `(foo() ``-` `5``); ` `    ``if` `(i < ``22``): ` `        ``if``(i < ``0``): ` `            ``return` `(i ``%` `7` `-` `7``) ``+` `1``; ` `        ``else``: ` `            ``return` `(i ``%` `7``) ``+` `1``; ` `             `  `    ``return` `my_rand(); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``print``(my_rand()); ` ` `  `# This code contributed by PrinciRaj1992 `

## C#

 `// C# program to Generate integer from  ` `// 1 to 5 with equal probability ` `using` `System; ` `class` `GfG  ` `{ ` `  `  `// given method that returns 1 to 5 with equal probability  ` `static` `int` `foo()  ` `{  ` `    ``// some code here  ` `    ``return` `0; ` `}  ` `  `  `// returns 1 to 7 with equal probability ` `public` `static` `int` `my_rand()   ` `{  ` `    ``int` `i;  ` `    ``i = 5*foo() + foo() - 5;  ` `    ``if` `(i < 22)  ` `        ``return` `i%7 + 1;  ` `    ``return` `my_rand();  ` `}  ` `  `  `// Driver code ` `public` `static` `void` `Main () {  ` `  `  `    ``Console.Write(my_rand()+``"\n"``);  ` `} ` `}  `

## PHP

 ` `

Output:

```-4
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.