Created
March 23, 2021 17:56
-
-
Save salRoid/361c174748c1334ea8ac7aa0324462dc to your computer and use it in GitHub Desktop.
MakeMyTrip Interview Question || Create a biased dice to 2X the probability of getting an even number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int getRandomNumber() { | |
return (rand() % 48 + 7); | |
} | |
int castDice() { | |
int randomNumber = getRandomNumber(); | |
if (randomNumber % 2 == 0) | |
return (randomNumber % 6); | |
else | |
return ((randomNumber / 2) % 6); | |
} | |
int main() { | |
srand((unsigned) time(0)); | |
int testCases = 7; | |
int evenItems = 0, oddItems = 0; | |
while (testCases--) { | |
int number = castDice(); | |
if (number == 0) | |
number = 6; | |
cout << number << endl; | |
if (number % 2 == 0) | |
evenItems++; | |
else | |
oddItems++; | |
} | |
cout << evenItems << " " << oddItems << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment