Created
June 2, 2016 10:02
-
-
Save m-Py/744f439ab127f118ed15ab602ce49ede to your computer and use it in GitHub Desktop.
Generate an array of any length n returning an array that contains a random sequence with integers 0 to n-1
This file contains hidden or 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
| rndSequence = function(length) { | |
| options = length; | |
| option_sequence = []; | |
| for (var i = 0; i < options; i++) { | |
| if (option_sequence.length === 0) { | |
| var rnd = Math.floor(Math.random()*options); | |
| option_sequence.push(rnd); | |
| } | |
| else if (option_sequence.length > 0) { | |
| var rnd = Math.floor(Math.random()*options); | |
| while (option_sequence.indexOf(rnd) != -1) { | |
| var rnd = Math.floor(Math.random()*options); | |
| } | |
| option_sequence.push(rnd); | |
| } | |
| } | |
| return option_sequence; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
console.log(rndSequence(5)) // random sequence of length 5