The shortest approach (ES6):
// randomly generated N = 40 length array 0 <= A[N] <= 39
Array.from({length: 40}, () => Math.floor(Math.random() * 40));
More Related Contents:
- Get a random item from a JavaScript array [duplicate]
- How to randomize (shuffle) a JavaScript array?
- Generate unique number within range (0 – X), keeping a history to prevent duplicates
- Sampling a random subset from an array
- Creating array of length n with random numbers in JavaScript
- Convert JSON to array in Javascript
- Do not include in random number
- Creating a JavaScript global array with static elements?
- JavaScript “new Array(n)” and “Array.prototype.map” weirdness
- Convert array to JSON
- How to find if an array contains a specific string in JavaScript/jQuery?
- How to change value of object which is inside an array using JavaScript or jQuery?
- How might I extract the property values of a JavaScript object into an array?
- JavaScript runtime complexity of Array functions
- Array Like Objects in Javascript
- Why does changing one array alters the other?
- Group by array and add field and sub array in main array
- Javascript Array.prototype.filter() not working
- Perform .join on value in array of objects
- Deep comparison of objects/arrays [duplicate]
- Are JavaScript Arrays actually implemented as arrays?
- How to check if an array contains another array?
- Generate random password string with requirements in javascript
- Remove duplicate objects from an array using javascript
- How can I use optional chaining with arrays and functions?
- Strange behavior of an array filled by Array.prototype.fill()
- What does the Array method `reduce` do?
- Elegant method to generate array of random dates within two dates
- Convert string array to integer array
- Group javascript array object by same key