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() method 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.
<!DOCTYPE HTML> <html> <head> <title> create an array with random values </title> </head> <body style="text-align:center;"> <h1 style="color: green"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN" style="color:green;"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to generate the array."; function gfg_Run() { el_down.innerHTML = Array.from({ length: 10 }, () => Math.floor(Math.random() * 10)); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Create an array and put the values in it (like, 1 at index 0, 2 at index 1, 3 at index 2 in the same order by a loop.)
- Assign variable (tp) = length of the array.
- Run a loop on variable(tp).
- Inside loop use Math.random() and Math.floor() method 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.
<!DOCTYPE HTML> <html> <head> <title> create an array with random values. </title> </head> <body style="text-align:center;"> <h1 style="color: green"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN" style="color:green;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); for (var a = [], i = 0; i < 10; ++i) a[i] = i; // Array like[1, 2, 3, 4, ...] el_up.innerHTML = "Click on the button to generate the array."; function createRandom(arr) { var 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), Swaping is done. arr[tp] = tmp; } return arr; } function gfg_Run() { el_down.innerHTML = createRandom(a); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- Websites and Software that help HTML, CSS And JavaScript Developers
- How to Create a Dropdown List with Array Values using JavaScript ?
- How to create slider to map a range of values in JavaScript ?
- How to select a random element from array in JavaScript ?
- How to pick a random color from an array using CSS and JavaScript ?
- Convert minutes to hours/minutes with the help of JQuery
- How to search JSON tree with the help of jQuery ?
- How to create a hyperlink for values from an array in Angular 8?
- JavaScript Math random() Method
- JavaScript to generate random hex codes of color
- JavaScript | Random
- How to generate random number in given range using JavaScript?
- Generate random alpha-numeric string in JavaScript
- How to generate a random boolean using JavaScript ?
- How to Generate a Random Password using JavaScript ?
- JavaScript | array.values()
- How to get all unique values (remove duplicates) in a JavaScript array?
- JavaScript | Get all non-unique values from an array
- How to check all values of an array are equal or not in JavaScript ?
- Find all the combinations of the array values in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


