Created
March 24, 2013 07:28
-
-
Save bigthyme/5230906 to your computer and use it in GitHub Desktop.
Less Naive implementation of uniform distribution
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
/** | |
* Given a function which produces a random integer in the range 1 to 5, write | |
* a function which produces a random integer in the range 1 to 7. | |
*/ | |
//returns a random number between 1-5 (inclusive) | |
var random1to5 = function() { | |
return Math.ceil(Math.random() * 5); | |
}; | |
//find a number between 1-7 without Math.random | |
var random1to7 = function () { | |
var x = 22; | |
while (x > 21) | |
x = random1to5() + random1to5()*5 - 5; | |
var randomNum = 1 + (x % 7); | |
return randomNum | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment