30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



Simulate a seven-sided die using only five-sided

82


MR on March 24, 2008 |Edit | Edit

Could you please explain this a bit?..I mean i can throw the die only once per turn?

I mean what is the expected behavior?

andy78 on March 31, 2008 |Edit | Edit

the idea here: all sides of the die just should have similar probability to appear:

we throwing 7 times (for each side of 7-sided die) five-sided die and remember sides which got 5, i.e. "qualified" to next round. Then continue until single side left, i.e. got number from 7-side die. (If none got "5" we can either repeat or "qualify" those with "4" and so on).

desiNerd on April 22, 2008 |Edit | Edit

guys its simple... let me reframe the question in a different way-
write the code for rand7() (that will generate random nos from 0 to 6 each with equal probability) given a function for rand5().

Ans:

rand7()
{
return (rand5() + (rand5()%3));
}

Anonymous on April 22, 2008 |Edit | Edit

u r right nerd

Ghost rider on April 26, 2008 |Edit | Edit

This is wrong man..
bcoz, die means it is supposed to generate unbiased numbers.
So, rand5() generates unbiased numbers in 0-4 ( meaning each number with equal probability) . But rand5()%3 will be biased bcoz:

using rand5()%3,

rand5():
0 1 2 3 4
|
v
rand5()%3:
0 1 2 0 1

So, 0,1 have higher probability comapred to 2.So the die becomes biased and that die is wrong
meaning finally :
rand7() will not generate all numbers with equal probability which it is supposed to g4enerate. menaing your rand7() function is wrong

loonyCoder on October 25, 2008 |Edit | Edit

Good catch GhostRider!

hoa on April 24, 2008 |Edit | Edit

is it that simple?

algooz on April 24, 2008 |Edit | Edit

1. Generate random bits, by calling rand5 until we get something other than 5
2. Output the value mod 2.
3. For generating rand7, call steps 1 and 2 three times, so that you get three random bits and treat them as three-bit binary numbers(range 0..7).
4. Do step 3 until you dont get a 0, then output the value.

Addy on September 24, 2009 |Edit | Edit

Looks good and different too :-)
0 1 2 3 4 5 => 6 numbers
by Rand5()%2 the probabilities of getting a 0 and a 1 are equal
P(getting a 0) == P(getting a 1) = 3/6 =1/2

now we have to generate a 3(_ _ _)digit number with these 0 and 1s which are already having a proper distribution.

Rand7()
return (Rand5()%2)*4+(Rand5()%2)*2+(Rand5()%2)*1

good job algooz.

Addy on September 24, 2009 |Edit | Edit

Looks good and different too :-)
0 1 2 3 4 5 => 6 numbers
by Rand5()%2 the probabilities of getting a 0 and a 1 are equal
P(getting a 0) == P(getting a 1) = 3/6 =1/2

now we have to generate a 3(_ _ _)digit number with these 0 and 1s which are already having a proper distribution.

Rand7()
return (Rand5()%2)*4+(Rand5()%2)*2+(Rand5()%2)*1;

good job algooz.

Addy on September 24, 2009 |Edit | Edit

Looks good and different too :-)
0 1 2 3 4 5 => 6 numbers
by Rand5()%2 the probabilities of getting a 0 and a 1 are equal
P(getting a 0) == P(getting a 1) = 3/6 =1/2

now we have to generate a 3(_ _ _)digit number with these 0 and 1s which are already having a proper distribution.

Rand7()
return (Rand5()%2)*4+(Rand5()%2)*2+(Rand5()%2)*1;

good job algooz.

Addy on September 24, 2009 |Edit | Edit

Looks good and different too :-)
0 1 2 3 4 5 => 6 numbers
by Rand5()%2 the probabilities of getting a 0 and a 1 are equal
P(getting a 0) == P(getting a 1) = 3/6 =1/2

now we have to generate a 3(_ _ _)digit number with these 0 and 1s which are already having a proper distribution.

Rand7()
return (Rand5()%2)*4+(Rand5()%2)*2+(Rand5()%2)*1;

good job algooz.

Addy on September 24, 2009 |Edit | Edit

Looks good and different too :-)
0 1 2 3 4 5 => 6 numbers
by Rand5()%2 the probabilities of getting a 0 and a 1 are equal
P(getting a 0) == P(getting a 1) = 3/6 =1/2

now we have to generate a 3(_ _ _)digit number with these 0 and 1s which are already having a proper distribution.

Rand7 => Rand5%2*4+Rand5%2*2+Rand5%2*1;

good job algooz.

ms_emp on June 07, 2008 |Edit | Edit

Let's say that 5 sided die generates numbers from 0 to 4.
Then throw the die for 7 times and get the total value of each die.
We will get one of 0, 1, 2, 3, ..., 28.
The permutation of 0 happen is 1.
The permutation of 1 happen is 7.
....
The permutation of 27 happen is 7.
The permutation of 28 happen is 1.
(Symmetric)

Get to total value of all the permutations and devide by 7.
Let's say the value from above is V.
Get 7 combination to get V.
Each of 0 to 28 must be used, no duplicat but number of numbers does not matter.
We get unviased 7-sided die.

varun_agg on June 12, 2008 |Edit | Edit

It can be done in two throws:
Roll the die twice: and simply take XOR of result1 and ~result2.
Explanation:
from 1-5 the three bits have certain probability for 1 on them. By taking 1's complement of the second throw, u find make the probability same for 1 and 0 on each bit. So, 1-7 has same probability.

Anonymous on July 04, 2008 |Edit | Edit

how about result1=result2? then you got a 0. But 0 is not in 1-7?

Anonymous on July 04, 2008 |Edit | Edit

how about result1=result2? then you got a 0. But 0 is not in 1-7?

Anonymous on July 04, 2008 |Edit | Edit

how about result1=result2? then you got a 0. But 0 is not in 1-7?

Anonymous on July 04, 2008 |Edit | Edit

how about result1=result2? then you got a 0. But 0 is not in 1-7?

Bill on July 04, 2008 |Edit | Edit

how about result1=result2? then you got a 0. But 0 is not in 1-7?

Bill on July 04, 2008 |Edit | Edit

how about result1=result2? then you got a 0. But 0 is not in 1-7?

vvshah on July 15, 2008 |Edit | Edit

Throw the dice seven times, number each trial from one to seven, and pick the one with maximum number face-up.
If there is a tie, throw tied ones again to break the tie.
Since all dice are identical, the outputs are distributed uniformly from 1 to 7.
I think andy78 means the same thing bus hasn't explained well.
Even algooz's answer looks good.

lensbo on July 18, 2008 |Edit | Edit

hi,guys,the followings are the correct solutions.
Throw the dice5 for 7 times, and sum the number we get. The sum will be a number between 7 and 35, with the same probility. Then, get the modul of the sum devided by 7, which is a number from 0 to 6. Just plus 1, we can get a dice7.

Anonymous on August 27, 2008 |Edit | Edit

This is not correct!! From 7 to 35, there are 29 numbers. You cannot modulo them evenly to 0 - 6. The probability for 0 is bigger!!!

Chetan on August 29, 2008 |Edit | Edit

Call the function that generates the random numbers between 1 to 5 twice and add these two results and return modulo 7 of the sum.

Fabregas! on August 30, 2008 |Edit | Edit

NO.........THAT WONT BE A GOOD RANDOM FUNCTION.......
BECAUSE THE SUM OBTAINED FOR A PARTICULAR NUMBER CAN BE FROM DIFFERENT COMBINATIONS.e.g.:

2+2,1+3,3+1 = 4
2+1, 1+2 =3

which means the probability of getting 4 is more than 3

Fabregas! on August 30, 2008 |Edit | Edit

int rand7()
{
int i, sum;

for(i = 0, sum = 0; i < 5; ++i)
sum += rand5();

/* sum is in [5, 25] now, but that is not
* a uniform distribution, so subtract 2
* to put it in [3, 23] so that division by
* 3 gives us a nice even [1, 7].
*/

return (sum - 2) / 3;
}

Fabregas! on August 30, 2008 |Edit | Edit

wrong!......i am sorry........even this is wrong

Dipak on August 31, 2008 |Edit | Edit

what if I take modulus of rand5() function?
-mod[rand5()*7]

Dipak on August 31, 2008 |Edit | Edit

forgot to mention modulus 5
i.e. Mod 5 of Rand5()*7

uday on August 31, 2008 |Edit | Edit

int rand7(){
return (rand5()+ rand5()+...7 times) % 7
}

cooldude on December 22, 2008 |Edit | Edit

i think this is wrong solution. rand5() if we call 7 times, we get between 7 and 35. there are 29 numbers between them. so it is not uniform distribution

Tuatha'an on September 01, 2008 |Edit | Edit

r1=ran15
r2=ran15
r3=ran15
r4=ran15

ranum=(r1+10*r2+100*r3+1000*r4) - 1111

return(Floor(ranum/635)+1)

Explanation:
number of possible values is 4444-0+1=4445. 4445/635 =7
we'll have a range of [0,4444]
min is 0+1=1
max is 4444/635=6.999=6+1=7

T.C on September 01, 2008 |Edit | Edit

I gave same answer as uday (after a lot f confusion) not as easy as it seem but i think: return (rand5()+ rand5()+...7 times) % 7 is OK (truely random)

as far as Tuatha's explanation .. hopefully it's doesn't need to be that complex, i can't imaging coming up with that stuff while on a phone interview.

T.C on September 01, 2008 |Edit | Edit

Actually that would give 0-6 so need to add 1
so:
int rd7=0
for(int i=0;i!=7;i++)
rd7+=random5();
rd7=1+rd7%7
return rd7;

I think that would work fine, anybody think not ?

acoder on September 01, 2008 |Edit | Edit

in the above scheme:
let p be the probability of getting a any number between 1 & 5 using rand5. then probability of getting a 5 using rand7 == probability (rd5+rd5..7times)%7 is 4 => numerator can be 4,11,18,25,32 => probability depends upon the number of ways in which you can get a 4 or 11 or 18 ... If then number of ways is x, then the needed probability is xp.

probability of getting a 7 using rand7 can be computed similarly - its the probability that the term is 6 => numerator can be 6,13,20,27,34 => probability is the number of ways in which you can get 6 or 12 or 20 ...If ys is the number of ways, the needed prob is yp.

Is xp == yp ?

consider 4, Lets say there are n ways to get 4. Then to each such way, you can increment two of the 7 terms by 1 and get 6. You can choose these two terms in 21 ways! Thus the number of ways to get a 6 is more than then number of ways to get a 4. This shows that its not likely that xp = yp which means that the suggested method is non-uniform :(

I'm not sure about the above argument! What do you think ?

acoder on September 01, 2008 |Edit | Edit

Heres another scheme -
(rand5-1)*6/4 + 1

explanation:
subtracting by 1 gives a random# between 0 & 4. Dividing by 4 gives a random# between 0 & 1. Multiplying by 6 gives a random number between 0 & 6. Adding 1 to this gives a random number between 1 & 7.

FK on October 09, 2008 |Edit | Edit

according to ur formula, we can only have following values:
1
2.5
4
5.5
7

if you floor decimal values 3 and 6 will never appear and if u decide to ceil decimal then 2 and 5 will never appear

Anonymous on September 01, 2008 |Edit | Edit

How come summation become uniform distribution? It is becoming Welghted..
I think we can improve the range with complete uniform solution... But we can use rejection... for example:

bindas on September 01, 2008 |Edit | Edit

How come summation become uniform distribution? It is becoming Welghted..
I think we can improve the range with complete uniform solution... But we can use rejection... for example:
---------
int rand7(){
int x = 21;
while( x > 20){
x = 5*(rand5()-1) + (rand5()-1);
}
int r = 1 + (x % 7);
return r;
}
---------
we can improve it if we take to 1-125 range...

T.C on September 01, 2008 |Edit | Edit

Hey, what about using shifting ?
function rndBit()
{
// equal prob. of 0 or 1
return rand5() & 1;
}

rand7= 1+ (rndBit()<<3 | rndBit()<<2 | rndBit()<<1 | rndBit()) & 6;

would not this give us random bits (not weighted) and work ?

onion834 on September 05, 2008 |Edit | Edit

T.C

- rndBit does not have equiprobability to produce 0 and 1. 1,3,5 will return 1 and 2,4 will return 0.

- do we need rndBit()<<3 here as it is ANDed with 6(0x110)?

- adding 1 and ANDing with 6 will always result in 0 in LSB. so it won't generate odd numbers.

onion834 on September 05, 2008 |Edit | Edit

I couldn't find an answer myself. :(

Ravi Savaliya on September 07, 2008 |Edit | Edit

int evenrand()
{
return (rand()%4)*2;
}
rand()
{
//returns no between 1 to 5
}

Joon on September 11, 2008 |Edit | Edit

The point of the question is equal distribution. Think about two dimension array (rand(), rand()). This will generate total 25 cases: (1,1), (1,2), ..., (5,5).

In case of (1,1) return 1.
In case of (1,2) return 2.
In case of (1,3) return 3.
In case of (1,4) return 4.
In case of (1,5) return 5.
In case of (2,1) return 6.
In case of (2,2) return 7.
In case of (2,3), in this case, return 1.
Then (2,4) returns 2. And make it recursively until you hit the (5,1). In case of (5,2), (5,3), (5,4) and (5,5) run this logic again. In this way, we can obtain equal distribution.

Satish on September 18, 2008 |Edit | Edit

If the uniformity of numbers among random numbers from 1 to 7 is the most important.

Clacualte:
factor = GCD( <1st_Number>, <2nd_Number> )
iff ( GCD is same as { <1st_Number> or <2nd_Number> )
lcmNo = LCM( <1st_Number>, <2nd_Number> )
factor = lcmNo
Generate 1....factor random numbers of range <1...1stnumber>
Sum them and take modulo of <2nd_Number>

CyberPhoenix on September 23, 2008 |Edit | Edit

int rand7(){
int random7= 0;
for(int i =0;i<7;i++){
random7 += rand5();
}
return (random7%7);
}

coder on September 25, 2008 |Edit | Edit

algooz is absolutely correct... his solution is perfect...!!!!

som on October 02, 2008 |Edit | Edit

I believe acoder's solution is correct. It will help if someone can argue against it.

FK on October 09, 2008 |Edit | Edit

see reply to that comment

nanmaga on October 09, 2008 |Edit | Edit

Why not something like this:

1) From R5(), we can generate R2() with uniform distribution. How?...
1-ret 1
2-ret 1
3 ret 2
4 ret 2
5 repeat R2()

Modification of this would be

R34()
1-ret 3
2-ret 3
3-ret 4
4-ret 4
5-repeat R34()

Similarly for 5,6 with R56()

Now, to simulate R7(),

1-ret R12()
2-ret R34()
3-ret R56()
4-ret 7
5-repeat R7()

This should be uniformly distributed....anybody thinks otherwise?

loonyCoder on October 25, 2008 |Edit | Edit

This solution is inspired from Nanmaga...
Throw the dice twice and do the following:

function rand7() {
throw the dice twice and perform the following checks

1,1 return 1
1,2 return 2
1,3 return 3
1,4 return 4
1,5 return 5
2,1 return 6
2,2 return 7
2,3 return 1
2,4 return 2
2,5 return 3
3,1 return 4
...
...
...
5,1 return 7 [at this point we have returned numbers from 1 to 7, 3 times each]
5,2 rand7()
5,3 rand7()
5,4 rand7()
5,5 rand7()
}

In short, in the last 4 cases, we are subdividing the remaining probability into equal proportions by calling rand7() again.

Any idea if this solution sounds okay?

Rohith Menon on October 31, 2008 |Edit | Edit

do rand5()+rand5()
2(1,1) 3(1,2) 3(2,1) 4(1,3) 4(2,2) 4(3,1) 5 5 5 5 6 6 6 6 6 7 7 7 7 8 8 8 9 9 10(5,5)

if
1,1 = return 1
1,2 = return 2
2,1 = return 3
1,3 = return 4
2,2 = return 5
3,1 = return 6
5,5 = return 7

For all other combinations repeat rolling the dies till one of the above combinations come.

Probability calculation:-
Selection probability for the above case = 1/25.
Rejection probability = 18/25

1/25 + (18/25)*1/25 + (18/25)^2*1/25 + ....

1/25*(18/25 + (18/25)^2 + (18/25)^3 +....)

1/25*(25/7)
= 1/7

Comments/criticisms welcome.

Since this thread is very long, i apologize if some1 has already posted this solution.

Thanks,
Rohith Menon.

Anonymous on July 16, 2009 |Edit | Edit

This is the right solution, based on conditional probability and algebra. Thank you so much.

Anonymous on September 06, 2009 |Edit | Edit

what non sense

see bro if you wish to consider failure take 18/25 and sucess take 7/25

do not mix 1/25 with 18/25 because both probability are for different.
1/25 is sucess for one case then fail is 24/25

selekt on November 11, 2008 |Edit | Edit

common solution I saw in some other post:

look at the end bits of the numbers generated by 5:
000
001
010
011
100
101

the lsb is generated wth equal probablity ... hence .. all we need to do is call rand5() 3 times and keep on appending the results to a variable num .. to get our value between 0 and 7

selekt on November 11, 2008 |Edit | Edit

I meant the lsb of the result to the variable num

ta on November 25, 2008 |Edit | Edit

Well, you could hardly get 000 in one throw...

Anonymous on November 16, 2008 |Edit | Edit

This is a trick question and there is no correct solution. There are only solutions which are close to correct. The reason for this is that 5 and 7 are relatively prime (also called coprime), so no mapping between them is possible. This means that any solution either (a) does not have the correct probability, or (b) has the possibility of never terminating. All of the solutions given above are therefore incorrect. Lest you think that you can simply convert to binary and back, note that 2 is also relatively prime to both 5 and 7.

I was once asked this question in a phone interview and I tried to explain to the questioner why it was an invalid question, but he didn't understand what "relatively prime" meant.

http://mathworld.wolfram.com/RelativelyPrime.html
http://en.wikipedia.org/wiki/Coprime

ta on November 25, 2008 |Edit | Edit

Throw the dice 5 times. Calculated the sum of all the throws. The sum will be in the range [5, 25], i.e. 21 different results. For the different results do the following

Result 7-side simulation result
------ ------------------------
5,6,7 1
8,9,10 2
11,12,13 3
14,15,16 4
17,18,19 5
20,21,22 6
23,24,25 7

ta on November 25, 2008 |Edit | Edit

That formatting did not work, it took all the blanks away. Probably some sort of automatism that SW does so nicely. I will register a trademark for that kind of stuff "Automatically Wrong".

Result 7-side simulation result
------ ------------------------
5,6,7 ====> 1
8,9,10 ===> 2
11,12,13 => 3
14,15,16 => 4
17,18,19 => 5
20,21,22 => 6
23,24,25 => 7

RDK on November 30, 2008 |Edit | Edit

Easy:
rand7 = ((ran5 + rand5 + rand5 + rand5 + rand5 - 5) % 7) + 1

The 5 rand will give range 5-25 : 21 numbers.

Equal probability for all.

Anonymous on May 07, 2009 |Edit | Edit

not uniform distribution, dude.

ShahShadow on June 03, 2009 |Edit | Edit

The solution from lensbo would seem to work if you throw the 5-sided die 14 times. The sum of the die throws will get you a number from 14 to 70 where the range is 56 (a multiple of 7). Take the sum modulo 7, and you get a number from 0 to 6 evenly distributed. Add 1, and you have a 7-sided die.

Joe Blow on July 27, 2009 |Edit | Edit

function rand7() {
v3 = 7;
while (v3 > 6) {
v1 = 4;
while (v1 > 3) {
v1 = rand5();
}
v2 = 4;
while (v2 > 3) {
v2 = rand5();
}
v3 = (v2 << 2) | v1;
}
return v3;
}

" on August 11, 2009 |Edit | Edit

why not just sum the result of two rolls, and if it is bigger then 6 try again?


function rand7(){
X1=rand5();
X2=rand5();
X3=x1+x2;
If result>6 return rand7(); // try again
return result;
}

try again on August 11, 2009 |Edit | Edit

why not just sum the result of two rolls, and if it is bigger then 6 try again?


function rand7(){
X1=rand5();
X2=rand5();
X3=x1+x2;
If result>6 return rand7(); // try again
return result;
}

this is correct. on August 20, 2009 |Edit | Edit

this is correct, though result should be replaced by x3

The above is not correct. on August 20, 2009 |Edit | Edit

suppose rand5() returns any number in {1, 2, 3, 4, 5}.

int rand7(){
int X1=rand5();
int X2=rand5();
int result = X1 + X2; //If rand5() returns {0, 1, 2, 3, 4} then {0, 3, 4} must be excluded.
If result >= 8 return rand7(); // try again
return result;
}

Anonymous on November 12, 2009 |Edit | Edit

same here :)

Arvind on September 03, 2009 |Edit | Edit

i think we can follow this approach:

basically we need three diff bits to mkae those (8) numbers then we could discard 000 from there.
so what we need is a routine whcih generates 0 and 1 with equal probability
int rand2()
{
while(1)
{
int randomNum = rand5();
return randomNum%2; // (equal numer of cases of 1 and 0)
}
}

Main routine to generate 8 randomnumbers btwn (0to 7)

int rand7(

int bit1 = rand2();
int bit2 = rand2();
int bit3 =rand2();
int finalNum = bit1<<2 | bit2<<1 | bit3
if(finalNum! =8)
return finalNum;
}

This will guarantee equal probabilitu for all (0 to 7)

your views?

wow on September 13, 2009 |Edit | Edit

surprised no one has found the "correct"/optimal answer. Took me 3 days to solve it and its an elegant solution. One guy I saw was close.

here's some hints, how do you turn a 10 sided die to a 7 sided die?
And then how do you turn a 2 sided die (a coin) into a 4 sided die?

jaiHo on September 30, 2009 |Edit | Edit

are you alluding to the fact that we can toss the coin twice and get 4 different cases?
and in a 10 sided die, you can neglect any three predefined cases?

in such a case (like 10 and 7) there is a chance that you will be in a non-terminating experiment.

if you know the answer, why not just tell rather than act f'cool..

sg on December 12, 2009 |Edit | Edit

rand5()+randr()+.....12times..
This gives numbers between 12 to 60 uniformly....
so there are 60-12+1 = 49 numbers in total.....
do mod 7 with result and add 1...
this gives 1-7 with equal prob!
same with dice...can be thrown 12 times..n add the results % 7..

XeqtR on February 05, 2010 |Edit | Edit

Change each five-sided die into a binary digit (1-2 are zero, 3-4 are 1, five is a re-roll) and then roll three of them. Interpret the number as binary, with zero being a re-roll
PS: Found this on someone s blog about their google interview experience

RahulParundekar on March 02, 2010 |Edit | Edit

I am not sure if this is correct-
Result Set contains [000,001,010,011,100,101,110,111] with equal probability. This is something of a Rand8();

Fanu on May 22, 2010 |Edit | Edit

hmmm yea but 000 is 0 so that is a re-roll. so the remaining set will be [001,010,011,100,101,110,111].

RahulParundekar on March 02, 2010 |Edit | Edit

I think there is no possible solution; as there are no common powers of 5 and 7. (Note: rolling a die of 6 sides twice, gives 6^2 = 36 possibilities such that (6^2)%6=0 and a number occuring has equal probabilities from 1 through 6. Thus for this to work with 5 and 7 we need a number x such that (5^x)%7=0. Such a number does not exist. Refer to post by anonymous on relative prime.

jbx on June 08, 2010 |Edit | Edit

Can't we simply do it like this:

floor(rand5() * 7 / 5)?

If you think a little how rand() works, it just gives you a float between 0 and 1, and we just multiply it to get a number within our range.

Anonymous on August 15, 2010 |Edit | Edit

int dice7() {
while (true) {
int num = 5 * (dice5() - 1) + (dice5() - 1);
if (num < 21) return (num % 7 + 1);
}
}

M on August 15, 2010 |Edit | Edit

int dice7() {
while (true) {
int num = 5 * (dice5() - 1) + (dice5() - 1);
if (num < 21) return (num % 7 + 1);
}
}

M on August 15, 2010 |Edit | Edit

this method is called Rejection Sampling. Although not perfect (which is not possible) it is close

Add a Text Comment | Add Runnable Code
Name:
Comment:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out