Implement rand3() using rand2()

Given a function rand2() that returns 0 or 1 with equal probability, implement rand3() using rand2() that returns 0, 1 or 2 with equal probability. Minimize the number of calls to rand2() method. Also, use of any other library function and floating point arithmetic are not allowed.

The idea is to use expression 2 * rand2() + rand2(). It returns 0, 1, 2, 3 with equal probability. To make it return 0, 1, 2 with equal probability, we eliminate the undesired event 3.



Below is the implementation of above idea –

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to print 0, 1 or 2 with equal 
// probability
#include <iostream>
using namespace std;
  
// Random Function to that returns 0 or 1 with
// equal probability
int rand2()
{
    // rand() function will generate odd or even
    // number with equal probability. If rand()
    // generates odd number, the function will
    // return 1 else it will return 0.
    return rand() & 1;
}
   
// Random Function to that returns 0, 1 or 2 with 
// equal probability 1 with 75%
int rand3()
{
    // returns 0, 1, 2 or 3 with 25% probability
    int r = 2 * rand2() + rand2();
      
    if (r < 3)
        return r;
      
    return rand3();
}
  
// Driver code to test above functions
int main()
{
    // Initialize random number generator
    srand(time(NULL));
   
    for(int i = 0; i < 100; i++)
        cout << rand3();
   
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to print 0, 1 or 2 with equal 
# Probability
  
import random
# Random Function to that returns 0 or 1 with 
# equal probability 
  
def rand2():
  
    # randint(0,100) function will generate odd or even
    # number [1,100] with equal probability. If rand()
    # generates odd number, the function will 
    # return 1 else it will return 0
    tmp=random.randint(1,100)
    return tmp%2
      
# Random Function to that returns 0, 1 or 2 with 
# equal probability 1 with 75% 
def rand3():
      
    # returns 0, 1, 2 or 3 with 25% probability 
    r = 2 * rand2() + rand2()
    if r<3:
        return r
    return rand3()
      
# Driver code to test above functions
if __name__=='__main__':
    for i in range(100):
        print(rand3(),end="")
          
#This code is contributed by sahilshelangia

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to print 0, 1 or 
// 2 with equal probability
  
// Random Function to that 
// returns 0 or 1 with
// equal probability
function rand2()
{
    // rand() function will generate 
    // odd or even number with equal 
    // probability. If rand() generates 
    // odd number, the function will
    // return 1 else it will return 0.
    return rand() & 1;
}
  
// Random Function to that 
// returns 0, 1 or 2 with 
// equal probability 1 with 75%
function rand3()
{
    // returns 0, 1, 2 or 3 
    // with 25% probability
    $r = 2 * rand2() + rand2();
      
    if ($r < 3)
        return $r;
      
    return rand3();
}
  
// Driver Code
  
// Initialize random 
// number generator
srand(time(NULL));
  
for($i = 0; $i < 100; $i++)
    echo rand3();
  
// This code is contributed by aj_36
?>

chevron_right



Output :

2111011101112002111002020210112022022022211100100121202021102100010200121121210122011022111020

Another Solution –

If x = rand2() and y = rand2(), x + y will return 0 and 2 with 25% probability and 1 with 50% probability. To make probability of 1 equal to that of 0 and 2 i.e. 25%, we eliminate one undesired event that’s resulting in x + y = 1 i.e. either (x = 1, y = 0) or (x = 0, y = 1).

int rand3()
{
    int x, y;

    do {
        x = rand2();
        y = rand2();
    } while (x == 0 && y == 1);

    return x + y;
}

Please note above solutions will produce different results every time we run them.

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :

1


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