How to create an array with random values with the help of JavaScript?


We can use JavaScript Math.random() method to generate random values. JavaScript provides us with different Array methods, such as from(), fill(), and push(), that can be used to create an array with random values.

Arrays in JavaScript are a powerful feature that allows you to store a collection of values of different data types. They are widely used in many applications, and having an array with random values can be very useful in certain scenarios.

An array of random values is a collection of randomly generated numbers stored in an array. The values in an array of random values can be generated using built-in functions or custom algorithms. The size of the array and the range of values can be specified as needed.

Using Math.random()

Math.random() is one of the most common built-in JavaScript methods that generate a random number between 0 (inclusive) and 1 (exclusive). It can be used to generate random numbers for various purposes, such as for simulations, games, and statistical applications.

We use a for loop and Math.random() method to create an array. Look at the syntax below −

Syntax

Following is the syntax to create an array with random values using a loop and Math.random() method.

for (let i = 0; i < 20; i++) {
   randomArr.push(Math.random());
}

The above syntax creates an array of twenty numbers between 0 and 1.

To create an array of random natural numbers we can use the following syntax −

for (let i = 0; i < 20; i++) {
   randomArr.push(Math.floor(Math.random() * 100) + 1);
}

Example

In the following example, we create an array of twenty random numbers between 1 and 100.

<html>
<body>
   <p id="array"></p>
   <script>
      let randomArr = [];
      for (let i = 0; i < 20; i++) {
         randomArr.push(Math.floor(Math.random() * 100) + 1);
      }
      document.getElementById("array").innerHTML = JSON.stringify(randomArr);
   </script>
</body>
</html>

In this example, we first create an empty array called randomArr. We then use a for loop to iterate 10 times, and in each iteration, we use the Math.random() function to generate a random number between 0 and 1. We then multiply this number by 100 and add 1 to get a number between 1 and 100. Finally, we use the Math.floor() function to round down the number to the nearest integer. We then use the push() method to add this random number to the randomArr.

The output gets changed as it produces random values every time the code gets executed.

Using Array.from() Method

The Array.from() method and map() method can be used together in JavaScript to create an array with values generated by a specified function. The Array.from() method creates an array from an array-like or iterable object, and the map() method can be used to apply a function to each element in the array.

Example

In this example, we pass two arguments to the Array.from() method: an object with a length property of 10 and a function that returns a random number between 1 and 100. The Array.from() method creates a new array with the given length and fills it with the result of calling the provided function.

<html>
<body>
   <p id="array"></p>
   <script>
      const randomArr = Array.from({length: 10}, () => Math.floor(Math.random() * 100) + 1);
      document.getElementById("array").innerHTML = JSON.stringify(randomArr);
   </script>
</body>
</html>

Using Array.fill() Method

The Array.fill() method is another way to create an array with random values in JavaScript. It allows you to fill an array with a static value or a value generated by a function. Let’s see how you can use the Array.fill() method to create an array with random values.

<html>
<body>
   <p id="array"></p>
   <script>
      let result = new Array(10)    
      result = result.fill(0).map(() => Math.floor(Math.random() * 100));
      document.getElementById("array").innerHTML = JSON.stringify(result);
   </script>
</body>
</html>

In conclusion, arrays of random values are a useful tool for many applications in programming, particularly in simulations, games, and statistics. JavaScript provides several methods for generating arrays of random values, including using the Math.random() method, for loops, the Array.fill() method, the Array.from() method with map(), and the Array constructor.

Each of these methods provides a way to create arrays of random values, and choosing the best method depends on the specific requirements of your project. The Math.random() function is a simple and straightforward way to generate random values, while the for loop provides more control over the generation of the values. The Array.fill() method and the Array.from() method with map() allow you to fill an array with values generated by a specified function, making it easy to generate arrays of random values.

Updated on: 23-Feb-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements