## Oracle Interview Question

Software Developers**Country:**India

Generic pseudo-code, not intended to compile. Not tested, but I think it should work.

```
function int myRandom() {
int roll1 = originalRandom();
int roll2 = originalRandom();
// Only 4 possible combinations, with equal probabilities:
// roll1 = roll2 = 0
// roll1 = roll2 = 1
// roll1 = 0; roll2 = 1
// roll1 = 1; roll2 = 0
if (roll1 == 0 && roll2 == 0) // 25% probability
return 0;
else // 75% probability
return 1;
}
```

```
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int rand50(){
srand(time(NULL));
int randInt= rand(); // randInt is between 0 and RAND_MAX
(x%2)==0? return 1 : return 0;
}
int rand75(){
int x = rand50(); // x is now one of {0, 1}
x = x << 1; // x is now one of {00, 10}
x = x ^ rand50(); // x is now one of {00, 01, 10, 11} in binaries
(x>0)? return 1 : return 0;
}
```

Pretty rusty with probabilities, so let me know your thoughts if this works out to be 75%

My first test, I rolled 12 times, got 9 rolls that returned 1, 3 rolls that return 0, gave me exactly: 9/12 = 75%

Sample size should be larger, so I should run more tests.

```
/* The lowest number of rolls to get a combination of 4 such that we can pick 3 out of 4
to give the 75% probability is through two rolls:
Roll Results
1: 0, 0
2: 0, 1
3: 1, 0
4: 1, 1
Pick 1 - 3 to return 1 for 75%
Pick 4 to return 0 for 25% probability
*/
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int random50()
{
int result = rand() % 2;
return result;
}
int random75()
{
const int size = 3;
int valuesFor1[size] = { 0, 1, 10 };
// First roll is for the 10's digit place
int roll1 = random50();
int roll2 = random50();
int result = (roll1 * 10) + roll2;
for (int i = 0; i < size; ++i)
{
if (result == valuesFor1[i])
{
return 1;
}
}
return 0;
}
int main()
{
srand (time(NULL));
int value = random75();
cout << value << endl;
return 0;
}
```

Generic pseudo-code, not intended to compile. Not tested, but I think it should work.

```
function int myRandom() {
int roll1 = originalRandom();
int roll2 = originalRandom();
// Only 4 possible combinations, with equal probabilities:
// roll1 = roll2 = 0
// roll1 = roll2 = 1
// roll1 = 0; roll2 = 1
// roll1 = 1; roll2 = 0
if (roll1 == 0 && roll2 == 0) // 25% probability
return 0;
else // 75% probability
return 1;
}
```

Generic pseudo-code, not intended to compile. Not tested, but I think it should work.

```
function int myRandom() {
int roll1 = originalRandom();
int roll2 = originalRandom();
// Only 4 possible combinations, with equal probabilities:
// roll1 = roll2 = 0
// roll1 = roll2 = 1
// roll1 = 0; roll2 = 1
// roll1 = 1; roll2 = 0
if (roll1 == 0 && roll2 == 0) // 25% probability
return 0;
else // 75% probability
return 1;
}
```

```
from random import randint
# given function
def rand_50() :
return randint(0,1)
# requested function
def rand_75() :
if (rand_50() + rand_50()) == 2 :
return 0
else :
return 1
# test distribution over n outcomes
def test(n) :
outcome = 0
for i in range(0,n) :
outcome = outcome + rand_75()
p_1 = outcome / n
p_0 = 1 - p_1
print("p_0= %s, p_1= %s " % (p_0, p_1))
test(10000)
```

Using bitwise OR:

- Roman July 23, 2016