4

Is there any method apart from array splicing that I can use to generate a random number between two numbers without repeating at all until all the numbers between those two numbers have been generated? Shuffling techniques or any other array methods apart from splicing would be extremely helpful.

1

3 Answers 3

2

First we use the fisherYates implementation (credit goes to @ChristopheD) and extend the array prototype to have a shuffle function available

function arrayShuffle () {
   var i = this.length, j, temp;
   if ( i === 0 ) return false;
   while ( --i ) {
      j = Math.floor( Math.random() * ( i + 1 ) );
      temp = this[i];
      this[i] = this[j]; 
      this[j] = temp;
   }
}

Array.prototype.shuffle =arrayShuffle;

var numbers = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
numbers.shuffle();

Now with the use of the pop method we get a number from our seed until it is empty

numbers.pop(); //returns a number

To make sure we have an array filled with numbers in the range of start and end we use a simple loop to create our seed.

var start = 1;
var end = 5;
var numbers = new Array();
for (var i = start; i <= end; i++) {
    numbers.push(i);
}

here is a sample on jsfiddle

UPDATE: put fisherYates algo to shuffle more efficient

Sign up to request clarification or add additional context in comments.

9 Comments

This is a bad (non-uniform) shuffling algorithm. To see that it cannot be an uniform shuffling just consider that there are N**N equally probable shufflings while the number of permutations of N elements is N! and that in general N**N is not an exact multiple of N!.
This is not the correct way to shuffle. Look up Fisher-Yates shuffle. You can shuffle each member only LATER on in the list. This way you get n! possible permutations, which is the correct number to hit each exactly once.
dear lord, it should not been an academic solution. But for gods sake I put a more efficient shuffling algorithm :)
@MatthaisLaug Your shuffling algorithm was incorrect. As in, it was biased. As in, it did not pick all shuffles equally often. That's what we're complaining about, because lack of correctness is very bad :)
the fisterYates is an algorithm for generating a random permutation of a finite set—in plain terms, for randomly shuffling the set but it might happen, that you are getting the same permutation once again, before all permutation have shown up. If you want to avoid that, you need to provide an array of array to the shuffle method to check if the new shuffled array has not yet been used
|
0

What I usually do when dealing with smaller arrays is sorting the array by random:

yourArray.sort(function() { return 0.5 - Math.random() });

Comments

-1

Try this http://jsbin.com/imukuh/1/edit:

function randRange(min, max) {
  var result = [];
  for (var i=min; i<=max; i++) result.push(i);
  return result.map(function(v){ return [Math.random(), v] })
    .sort().map(function(v){ return v[1] });
}

console.log(randRange(1,5));
// [4, 3, 1, 5, 2]
// [3, 5, 2, 4, 1]
// [1, 5, 2, 3, 4]
// [3, 2, 5, 1, 4]
// ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.