Skip to content

Instantly share code, notes, and snippets.

@bigthyme
Created March 24, 2013 07:28
Show Gist options
  • Save bigthyme/5230906 to your computer and use it in GitHub Desktop.
Save bigthyme/5230906 to your computer and use it in GitHub Desktop.
Less Naive implementation of uniform distribution
/**
* 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