create a array with random values in javascript -


how can create array 40 elements, random values 0 39 ? [4, 23, 7, 39, 19, 0, 9, 14 ...]

i tried using solutions here:

http://freewebdesigntutorials.com/javascripttutorials/jsarrayobject/randomizearrayelements.htm

but array little randomized. generates lot of blocks of successive numbers...

here's solution shuffles list of unique numbers (no repeats, ever).

for (var a=[],i=0;i<40;++i) a[i]=i;  // http://stackoverflow.com/questions/962802#962890 function shuffle(array) {   var tmp, current, top = array.length;   if(top) while(--top) {     current = math.floor(math.random() * (top + 1));     tmp = array[current];     array[current] = array[top];     array[top] = tmp;   }   return array; }  = shuffle(a); 

if want allow repeated values (which not op wanted) elsewhere. :)


Comments