Array.from works... here is an example with your code... randomArray = (length: number, max: number) => Array.from({length}) .map(() => Math.floor(Math.random() * max)); @Damjan is there a way to not repeat numbers? Keeping default optional argument when adding to command. How to exclude it from generating the number 0? floor to result to make it whole number . I don't know where you believe that version for ES6 works? Here is a ES6 function that allows a min and a max and will generate an array of unique values in random order that contain all the number from min to max inclusive: To subscribe to this RSS feed, copy and paste this URL into your RSS reader. It works like this: imagine the numbers 1:n are stored in array. Most efficient way to create a zero filled JavaScript array? In this case, since the array has 4 elements inside of it, the last element is at an index of 3. But, JavaScript arrays are best described as arrays. Values in arrays is total random, so when you will be use this snippets, it will different. Below is my solution. October 14, 2014, 9:02am #1. The standard solution is to generate a random number between 0 and n-1 where n is length of the array. (I'm not critizing the answer, just raising a flag for future readers. What city is this on the Apple TV screensaver? How should I handle the problem of people entering others' e-mail addresses without annoying them with "verification" e-mails? gambler's fallacy might believe that this next flip was less likely to Why does my halogen T-4 desk lamp not light up the bulb completely. Join Stack Overflow to learn, share knowledge, and build your career. The next random number in the sequence is then ((r-1 + nr) % n) + 1. You can get a randomly shuffled array of numbers from 0 through 39 just like this: No fuss with the logistics of it all- plus it's super readable and easy to understand :). By Stephen Bucaro. Filling an array with a random number generator . To clarify: that's a letter L, not the number 1, in "l = 40", "i < l" and "Math.random() * l". Hmm... Are you sure this was a correct reply, mate?