Skip to content

Instantly share code, notes, and snippets.

@SH4DY
Last active October 10, 2016 02:13
Show Gist options
  • Save SH4DY/bbb9951161a612af0767 to your computer and use it in GitHub Desktop.
Save SH4DY/bbb9951161a612af0767 to your computer and use it in GitHub Desktop.
rand5() - Weekly interview question from interview cake
/*
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