Skip to content

Instantly share code, notes, and snippets.

@salRoid
Created March 23, 2021 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salRoid/361c174748c1334ea8ac7aa0324462dc to your computer and use it in GitHub Desktop.
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.
#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