Skip to content

Instantly share code, notes, and snippets.

@bigthyme
Created March 24, 2013 07:09
Show Gist options
  • Save bigthyme/5230873 to your computer and use it in GitHub Desktop.
Save bigthyme/5230873 to your computer and use it in GitHub Desktop.
JS function to find random number between 1-7
/**
* 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 c = 0;
var x = random1to5() + c;
c = (c + 5) % 35;
console.log(x);
console.log((x % 7)+1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment