Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2023-08-24 22:17:03
forward
1826 people have browsed it

如何借助 JavaScript 创建具有随机值的数组?

We can use JavaScript Math.random() method to generate random values. JavaScript provides us with different array methods like from(), fill() and Push() which can be used to create arrays with random values.

Arrays in JavaScript are a powerful feature that allow you to store collections of values ​​of different data types. They are widely used in many applications, and arrays with random values ​​can be very useful in certain situations.

A random value array 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 and value range of the array can be specified as needed.

Use Math.random()

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

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

grammar

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

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

The above syntax creates an array containing 20 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);
}
Copy after login

Example

In the following example, we create an array of 20 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>
Copy after login

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

The output will vary because it generates random values ​​every time the code is executed.

Use Array.from() method

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

Example

In this example, we pass two parameters 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>
Copy after login

Use Array.fill() method

The Array.fill() method is another way to create an array with random values ​​in JavaScript. It allows you to populate an array with static values ​​or values ​​generated by a function. Let us see how to create an array with random values ​​using Array.fill() method.

<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>
Copy after login

In summary, arrays of random values ​​are a useful tool for many applications in programming, especially in simulations, games, and statistics. JavaScript provides several ways to generate arrays of random values, including using the Math.random() method, a for loop, the Array.fill() method, the Array.from() method with map(), and the Array constructor.

Each method provides a way to create an array 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 value generation. The Array.fill() method and the Array.from() method with map() allow you to easily generate an array of random values ​​by filling the array with values ​​generated by a specified function.

The above is the detailed content of How to create an array with random values ​​with the help of JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template