Given a function rand50() that returns 0 or 1 with equal probability, write a function that returns 1 with 75% probability and 0 with 25% probability using rand50() only. Minimize the number of calls to the rand50() method. Also, the use of any other library function and floating-point arithmetic are not allowed.
The idea is to use Bitwise OR. A bitwise OR takes two bits and returns 0 if both bits are 0, while otherwise, the result is 1. So it has 75% probability that it will return 1.
Below is the implementation of the above idea :
C++
#include <iostream>
using namespace std;
int rand50()
{
return rand () & 1;
}
bool rand75()
{
return rand50() | rand50();
}
int main()
{
srand ( time (NULL));
for ( int i = 0; i < 50; i++)
cout << rand75();
return 0;
}
|
Java
class GFG
{
static int rand50()
{
return ( int ) ( 10 * Math.random()) & 1 ;
}
static int rand75()
{
return rand50() | rand50();
}
public static void main(String[] args)
{
for ( int i = 0 ; i < 50 ; i++)
{
System.out.print(rand75());
}
}
}
|
Python3
from random import randrange
def rand50():
return ( int )(randrange( 0 , 2 )) & 1
def rand75():
return rand50() | rand50()
for i in range ( 0 , 50 ):
print (rand75(), end = "")
|
C#
using System;
public class GFG
{
static Random rand = new Random();
static int rand50()
{
return rand.Next(0, 2);
}
static int rand75()
{
return rand50() | rand50();
}
public static void Main( string [] args)
{
for ( int i = 0; i < 50; i++)
{
Console.Write(rand75());
}
}
}
|
PHP
<?php
function rand50()
{
return rand() & 1;
}
function rand75()
{
return rand50() | rand50();
}
srand(time(NULL));
for ( $i = 0; $i < 50; $i ++)
echo rand75();
?>
|
Javascript
<script>
function rand50() {
return Math.floor(Math.random() * 10) & 1;
}
function rand75() {
return rand50() | rand50();
}
for (let i = 0; i < 50; i++)
document.write(rand75());
</script>
|
Output
11101010110101011010000101011110100010111110101111
Time Complexity: O(1)
Auxiliary Space: O(1)
On similar lines, we can also use Bitwise AND. Since it returns 0 with 75% probability, we have to invert the result.
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// Bitwise AND
bool rand75()
{
return !(rand50() & rand50());
}
Below is the implementation of the above idea :
C++
#include <iostream>
using namespace std;
int rand50()
{
return rand () & 1;
}
bool rand75()
{
return !(rand50() & rand50());
}
int main()
{
srand ( time (NULL));
for ( int i = 0; i < 50; i++)
cout << rand75();
return 0;
}
|
Java
class GFG
{
static int rand50()
{
return ( int ) ( 10 * Math.random()) & 1 ;
}
static int rand75()
{
return (rand50() & rand50())^ 1 ;
}
public static void main(String[] args)
{
for ( int i = 0 ; i < 50 ; i++)
{
System.out.print(rand75());
}
}
}
|
Python3
from random import randrange
def rand50():
return (( int )(randrange( 0 , 2 )) & 1 )
def rand75():
return (rand50() & rand50())^ 1
for i in range ( 0 , 50 ):
print (rand75(), end = "")
|
C#
using System;
class GFG
{
static int rand50()
{
var rand = new Random();
return rand.Next(0, 2);
}
static int rand75()
{
return (rand50() & rand50()) ^ 1;
}
public static void Main( string [] args)
{
for ( int i = 0; i < 50; i++) {
Console.Write(rand75());
}
}
}
|
Javascript
function rand50()
{
return (Math.floor(Math.random() * 2) & 1);
}
function rand75()
{
return (rand50() & rand50())^1;
}
for ( var i = 0; i < 50; i++)
process.stdout.write(rand75().toString());
|
Output
11111111000111101111110011111110011110111111010111
We can replace Bitwise OR and Bitwise AND operators with OR and AND operators as well –
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// OR or AND operator
int rand75()
{
return !(rand50() && rand50());
// return rand50() || rand50()
}
We can also achieve the result using the left shift operator and Bitwise XOR –
C++
#include <iostream>
using namespace std;
int rand50()
{
return rand () & 1;
}
int rand75()
{
int x = rand50();
x = x << 1;
x = x ^ rand50();
return (x > 0) ? 1 : 0;
}
int main()
{
srand ( time (NULL));
for ( int i = 0; i < 50; i++)
cout << rand75();
return 0;
}
|
Java
class GFG
{
static int rand50()
{
return ( int ) ( 10 * Math.random()) & 1 ;
}
static int rand75()
{
int x = rand50();
x = x << 1 ;
x = x ^ rand50();
return (x > 0 ) ? 1 : 0 ;
}
public static void main(String[] args)
{
for ( int i = 0 ; i < 50 ; i++)
System.out.print(rand75());
}
}
|
Python3
from random import randrange
def rand50():
return ( int )(randrange( 0 , 2 )) & 1
def rand75():
x = rand50()
x = x << 1
x = x ^ rand50()
return 1 if (x > 0 ) else 0
for i in range ( 0 , 50 ):
print (rand75(), end = "")
|
C#
using System;
public class GFG
{
static Random rnd = new Random();
static int rand50()
{
return rnd.Next(2);
}
static int rand75()
{
int x = rand50();
x = x << 1;
x = x ^ rand50();
return (x > 0) ? 1 : 0;
}
static public void Main (){
for ( int i = 0; i < 50; i++)
Console.Write(rand75());
}
}
|
PHP
<?php
function rand50()
{
return rand() & 1;
}
function rand75()
{
$x = rand50();
$x = $x << 1;
$x = $x ^ rand50();
return ( $x > 0) ? 1 : 0;
}
srand(time(NULL));
for ( $i = 0; $i < 50; $i ++)
echo rand75();
?>
|
Javascript
<script>
function rand50()
{
return Math.floor((10 * Math.random())) & 1;
}
function rand75()
{
let x = rand50();
x = x << 1;
x = x ^ rand50();
return (x > 0) ? 1 : 0;
}
for (let i = 0; i < 50; i++)
{
document.write(rand75());
}
</script>
|
Output
10110100111011011110111100101111110111100001111111
Time Complexity: O(1)
Auxiliary Space: O(1)
Please note above solutions will produce different results every time we run them.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Jul, 2022
Like Article
Save Article