Skip to content

Instantly share code, notes, and snippets.

@bmoren
Created August 22, 2016 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoren/dad0f6abee6f9eae84600d02f4ed231b to your computer and use it in GitHub Desktop.
Save bmoren/dad0f6abee6f9eae84600d02f4ed231b to your computer and use it in GitHub Desktop.
non repeating random, more efficiently.
var pRandom = 1;
var total = 5; // top end of range
var rand = Math.floor( Math.random()*total ); //get a rand
while(rand == pRandom){ //are we the same, if so try again until we are not.
rand = Math.floor( Math.random()*total ); //get a new random if we are the same
console.log("try again");
}
pRandom = rand; //store the current random number to check against next time.
console.log(rand); // output / do something with the #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment