Also if there is a better way of accomplishing this, I would love to learn/know that way as well.
There is.
If you need unique values, generate a regular sequential array [1,2,3,4] (or however long you need), and then use that to fill a second array, by successively extracting random elements from the first array (thus growing array 2, and shrinking array 1):
var a1 = [];
for (var i=0; i<4; i++) { a1.push(i); }
var a2 = [];
while (a1.length) {
var pos = Math.random()*a1.length;
var element = a1.splice(pos, 1)[0];
a2.push(element);
}
// a2 is now an array with a random permutation of the elements of a1
The splice
call removes an subarray starting at position pos
with (in this case) 1 element, so a1
gets shorter and shorter, until it's empty, at which point a2
will be a permuted array of unique values.
If we start with a1 = [0,1,2,3]
then after this runs a2
can be any of 24 possible sequences, with guaranteed unique values because that's what we started with, just in a randomised order.