Is there an efficient way to expand an integer into random, unique numbers?
I want to 10become 3 4 6 1 0 2 5 7 8 9.
I tried THIS ... but before pushing the values into arrays and going crazy, I thought there might have been 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)
EDIT:
AND HERE 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);
OR THIS too
Alright, I'm done.
source
share