How to create an array with random values with the help of JavaScript ? Last Updated : 07 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The task is to generate an array with random values with the help of JavaScript. There are two approaches which are discussed below: Approach 1: Use Math.random() and Math.floor() methods to get the random values.Push the values one by one in the array (But this approach may generate repeated values). Example: This example implements the above approach. JavaScript function gfg_Run() { console.log(Array.from({ length: 10 }, () => Math.floor(Math.random() * 10))); } gfg_Run() Output[ 8, 6, 4, 3, 9, 2, 8, 9, 7, 8 ] Approach 2: Create an array and put the values in it (like 1 at index 0, 2 at index 1, and 3 at index 2 in the same order by a loop.)Assign a variable (tp) = length of the array.Run a loop on variable(tp).Inside the loop use Math.random() and Math.floor() methods to get the random index of the array.Swap this array value with the index(tp) and decrease the variable(tp) by 1.Run the loop until variable(tp) becomes 0. Example: This example implements the above approach. JavaScript let a = [] for ( i = 0; i < 10; ++i) a[i] = i; // Array like[1, 2, 3, 4, ...] function createRandom(arr) { let tmp, cur, tp = arr.length; if (tp) // Run until tp becomes 0. while (--tp) { // Generating the random index. cur = Math.floor(Math.random() * (tp + 1)); // Getting the index(cur) value in variable(tmp). tmp = arr[cur]; // Moving the index(tp) value to index(cur). arr[cur] = arr[tp]; // Moving back the tmp value to // index(tp), Swapping is done. arr[tp] = tmp; } return arr; } function gfg_Run() { console.log(createRandom(a)); } gfg_Run() Output[ 0, 9, 4, 3, 6, 8, 2, 7, 1, 5 ] Comment More infoAdvertise with us Next Article How to create an array with random values with the help of JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Create a Dropdown List with Array Values using JavaScript? To add elements to the select element from the JavaScript array, We can also get selected values in the dropdown list using JavaScript. Here we will populate the dropdown list with an array.Below are the methods to create a dropdown list with array values:Table of ContentUsing Pure JavaScriptUsing j 2 min read Create an array filled with given values Using JavaScript In JavaScript, an array is a collection of values that can be stored and accessed in a single variable. Each value in an array is called an element, and elements can be of any data type, such as numbers, strings, or other objects. Arrays are indexed, meaning that each element has a numerical positio 5 min read How to create dynamic length array with numbers and sum the numbers using JavaScript ? In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. Unlike most languages where the array is a reference to the multiple variables, in JavaScript array is a single variable 2 min read How to Shuffle the Elements of an Array in JavaScript? Shuffle an array means randomly change the order of array elements. This is useful for creating random arrangements, like shuffling a deck of cards or randomizing a list. There are different wats to shuffle the elements of an array in JavaScript:1. Using Fisher-Yates (Knuth) ShuffleThis algorithm al 3 min read JavaScript - Select a Random Element from JS Array Selecting a random element from array in JavaScript is done by accessing an element using random index from the array. We can select the random element using the Math.random() function. How to select a random element from an array in JavaScript?1. Using Math.random() MethodThe Math.random() method i 2 min read How to Generate a Random Boolean using JavaScript? To generate a random boolean in JavaScript, use Math.random(), which returns a random number between 0 and 1.Approach: Using Math.random() functionCalculate Math.random() function.If it is less than 0.5, then true otherwise false.Example 1: This example implements the above approach. JavaScript// Fu 1 min read How to pick a random color from an array using CSS and JavaScript ? The task is to pick a random color from an array using CSS. There is no way to do this using CSS only. CSS doesn't support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array. Below 1 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read How to Generate a Random Password using JavaScript? Creating a random password generator is an excellent beginner-friendly project that can be useful in real-world applications. Passwords are vital for securing accounts, and strong, randomized passwords enhance security significantly. How to Generate a Random Password using JavaScriptWhat We're Going 5 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Like