Generate integer from 1 to 7 with equal probability

Last Updated : 19 Mar, 2026

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.

Examples:

Input: foo() - returns 1 to 5
Output: Generate numbers from 1 to 7 with equal probability.
Explanation: Generate values from 1 to 25, keep values less than 22, and map them to 1 to 7 using modulo.

Input: foo() - returns 1 to 5
Output: 4

[Approach] Using Rejection Sampling - O(1) Time and O(1) Space

The idea is to generate a value using 5 * foo() + foo() - 5, check if it is less than 22, and convert it to a number from 1 to 7 using modulo; otherwise, repeat the process until a valid value is obtained.

C++
// C++ program to Generate integer from 
// 1 to 5 with equal probability
#include <stdio.h>

// 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"); 
}
} 
JavaScript
<script>

// Javascript program to Generate integer from 
// 1 to 5 with equal probability

    // given method that returns 1 to 5 with equal probability 
    function foo()
    {
        // some code here 
        return 0;
    }
    
    // returns 1 to 7 with equal probability
    function my_rand()
    {
        let i; 
        i = 5*foo() + foo() - 5; 
        if (i < 22) 
            return i%7 + 1; 
        return my_rand(); 
    }
    
    // Driver code
    document.write(my_rand());
    
    // This code is contributed by rag2127
    
</script>
PHP
<?php
// PHP program to Generate integer from 
// 1 to 5 with equal probability
// given method that returns 1 to 5 
// with equal probability
function foo() 
{
    // some code here
}

// returns 1 to 7 with equal probability
function my_rand() 
{
    $i;
    $i = 5 * foo() + foo() - 5;
    if ($i < 22)
        return $i % 7 + 1;
    return my_rand();
}

// Driver code
echo my_rand();

// This code is contributed by Tushil.
?>

Output
1 
Comment