Last active
October 10, 2016 02:13
-
-
Save SH4DY/bbb9951161a612af0767 to your computer and use it in GitHub Desktop.
rand5() - Weekly interview question from interview cake
This file contains hidden or 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
/* | |
You have a function rand7() that generates a random integer from 1 to 7. | |
Use it to write a function rand5() that generates a random integer from 1 to 5. | |
rand7() returns each integer with equal probability. | |
rand5() must also return each integer with equal probability. | |
*/ | |
function rand5(){ | |
var x = rand7(); | |
while(x > 5){ | |
x = rand7(); | |
} | |
return x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment