Is there an efficient way to unwrap an integer into random, unique numbers?
I want 10
to become 3 4 6 1 0 2 5 7 8 9
.
I tried THIS ...but before pushing values into arrays and going loop crazy I thought maybe there was a better way.
EDIT:
HERE IS MY NEW FIDDLE AND FUNCTION:
function uniqueDigits(x){
y = [];
z = [];
while(x > 0) {
y.push(x);
x--;
}
while(y.length > 0){
var r = Math.floor(Math.random()*y.length);
var u = y[r]
y.splice(r, 1);
z.push(u-1);
}
return z;
}
uniqueDigits(4) //['2', '3', '0', '1']
EDIT:
AND HERE IS ANOTHER OPTION:
function uniqueNum(x){
z = y = x;
var r = Math.ceil(Math.random() * x);
while ( x%r%2 == 0 ) {
r = Math.ceil(Math.random() * x);
}
while( x>0 ){
y = y - r
if(y<0){
var n = y;
y = z + n
}
$('p').append(y);
x--
}
} uniqueNum(4);//['2', '3', '0', '1']
OR THIS one too
Ok I'm done.