How can I create a array with 40 elements, with random values from 0 to 39 ? Like [4, 23, 7, 39, 19, 0, 9, 14 ...]

I tried using solutions from here:

http://freewebdesigntutorials.com/javaScriptTutorials/jsArrayObject/randomizeArrayElements.htm

but the array I get is very little randomized. It generates a lot of blocks of successive numbers...

share|improve this question

7 Answers

up vote 3 down vote accepted
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;
}

a = shuffle(a);
share|improve this answer

Since the range of numbers is constrained, I'd say the best thing to do is generate the array, fill it with numbers zero through 39 (in order), then shuffle it.

share|improve this answer
Here's a visualization of this technique (although that page uses a terrible shuffle algorithm). – Phrogz Apr 29 '11 at 20:09
but how do I do that? – Alexandra Apr 29 '11 at 20:09
I don't know that he wanted every number between, but rather completely random? If he's fine with random order, static values then this will work fine. – Robert Apr 29 '11 at 20:10
which might do the job, but will give slightly different results than if you used Math.random() 40 times, since it will enforce each number appearing once and no repeats. – Mu Mind Apr 29 '11 at 20:13

Math.random() will return a number between 0 and 1(exclusive). So, if you want 0-40, you can multiple it by 40, the highest the result can ever be is what you're multiplying by.

var arr = [];
for (var i = 0, l = 40; i < l; i++) {
    arr.push(Math.round(Math.random() * l))
}
document.write(arr);

http://jsfiddle.net/robert/tUW89/

share|improve this answer
1  
To clarify: that's a letter L, not the number 1, in "l = 40", "i < l" and "Math.random() * l". The font makes it hard to tell. – Mu Mind Apr 29 '11 at 20:17

Sequences of random items often contain blocks of successive numbers, see the Gambler's Fallacy http://en.wikipedia.org/wiki/Gamblers_fallacy

share|improve this answer

from the page suggested by @Phrogz

for (var i=0,nums=[];i<49;i++) nums[i]={ n:i, rand:Math.random() };
nums.sort( function(a,b){ a=a.rand; b=b.rand; return a<b?-1:a>b?1:0 } );
share|improve this answer
var myArray = [];
var arrayMax = 40;
var limit = arrayMax + 1;
for (var i = 0; i < arrayMax; i++) {
  myArray.push(Math.floor(Math.random()*limit));
}

This above is the traditional way of doing it but I second @Pointy and @Phrogz if you want to avoid duplicates in your array without having to do expensive computation

share|improve this answer

I needed something a bit different than what these solutions gave, in that I needed to create an array with a number of distinct random numbers held to a specified range. Below is my solution.

function getDistinctRandomIntForArray(array, range){
   var n = Math.floor((Math.random() * range));
   if(array.indexOf(n) == -1){        
    return n; 
   } else {
    return getDistinctRandomIntForArray(array, range); 
   }
}

function generateArrayOfRandomInts(count, range) {
   var array = []; 
   for (i=0; i<count; ++i){
    array[i] = getDistinctRandomIntForArray(array, range);
   };
   return array; 
}

I would have preferred to not create a loop that has the possibility to end up with a lot of unnecessary calls (if your count, and range are high and are close to the same number) but this is the best I could come up with.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.