Given a function random01Generator() that gives you randomly either 0 or 1, implement a function that utilizes this function and generate numbers between 0 and 6(both inclusive). All numbers should have same probabilities of occurrence. Examples:
on multiple runs, it gives
3
2
3
6
0
Approach : The idea here is to find the range of the smallest number greater than the given range. Here, since range is 6, decide that 2^3 is the smallest number greater than 6. Hence, k here will be 3. So, try forming a binary number of 3 characters each time and once all 3 characters are obtained, look up for the corresponding decimal representation of that binary number and return that value. For e.g., if it gives 0 in first random01 call, then 1 in next call and finally again 1 in next call, it can be said that binary number thus formed is 011 which is decimal representation of number 3 and hence, return 3. If in case it gives 111, which is 7 which out of range. So, just discard this number and repeat the whole process again until it gives the number in the required range. On calling random01Generator() 6 times, the probability of occurrence of numbers won’t be same. For e.g., for first time, the probability of occurrence of 0 is 1/2. Now, for second time, again the probability is 1/2 which makes total probability as 1/4. If this process is repeated 6 times by calling the random function 6 times, this will make the probability of occurrence of 0 as 1/(2^6). Similarly, the probability of occurrence of 1 will be greater than that of 0, of 2 will be greater than 1 and so on, following the binomial distribution pattern. Doing above way will ensure that all probabilities are equal as it is considering only that range in which one can generate those numbers and rejecting any cases giving us result greater than the maximum specified range. Hence, the above approach. Obviously, this idea can be extended, if there is some other random generator, say random0mGenerator() and there is need to generate number between 0 to n, where n>m. In this case, modify below function to incorporate m and n. Prerequisites : BigInteger in Java.
C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <bitset>
using namespace std;
class RandomImp {
public :
int random01Generator() {
return rand () % 2;
}
void random06Generator() {
int val = 7;
while (val >= 7) {
string res = "" ;
for ( int i = 0; i < 3; i++) {
res += to_string(random01Generator());
}
bitset<3> bits(res);
val = bits.to_ulong();
}
cout << val << endl;
}
};
int main() {
srand ( time (0));
RandomImp r;
r.random06Generator();
return 0;
}
|
Java
import java.math.BigInteger;
import java.util.Random;
public class RandomImp{
public int random01Generator() {
Random rand = new Random();
return rand.nextInt( 2 );
}
public void random06Generator(){
Random rand = new Random();
int val = 7 ;
while (val >= 7 ) {
String res = "" ;
for ( int i = 0 ; i < 3 ; i++)
res +=
String.valueOf(random01Generator());
BigInteger bg = new BigInteger(res, 2 );
val = bg.intValue();
}
System.out.println(val);
}
public static void main(String[] args){
RandomImp r = new RandomImp();
r.random06Generator();
}
}
|
Python3
import random
class RandomImp:
def random01Generator( self ):
return random.randint( 0 , 1 )
def random06Generator( self ):
val = 7
while val > = 7 :
res = ""
for i in range ( 3 ):
res + = str ( self .random01Generator())
bg = int (res, 2 )
val = bg
print (val)
if __name__ = = '__main__' :
r = RandomImp()
r.random06Generator()
|
C#
using System;
using System.Collections.Generic;
class RandomImp {
Random random = new Random();
public int random01Generator() {
return random.Next(0, 2);
}
public void random06Generator() {
int val = 7;
while (val >= 7) {
string res = "" ;
for ( int i = 0; i < 3; i++) {
res += random01Generator().ToString();
}
int bits = Convert.ToInt32(res, 2);
val = bits;
}
Console.WriteLine(val);
}
}
public class GFG {
static public void Main( string [] args) {
RandomImp r = new RandomImp();
r.random06Generator();
}
}
|
Javascript
class RandomImp {
random01Generator() {
return Math.floor(Math.random() * 2);
}
random06Generator() {
let val = 7;
while (val >= 7) {
let res = "" ;
for (let i = 0; i < 3; i++) {
res += this .random01Generator();
}
val = parseInt(res, 2);
}
console.log(val);
}
}
const r = new RandomImp();
r.random06Generator();
|
Time Complexity: O(1)
Space Complexity: O(1)
This question is inspired from this stackoverflow link
Please Login to comment...