Detailed explanation of how to find random numbers in js

小云云
Release: 2023-03-22 22:18:02
Original
2385 people have browsed it

This article mainly shares with you the detailed explanation of the method of finding random numbers in js. It mainly shares it with you in text and code. I hope it can help you.

Find a randomly selected value within a certain positive range:

值 = Math.floor(Math.random() * 可能值的总数 + 第一个可能的值)
Copy after login

For example:

1. Find a random integer within 1-100

var result = Math.floor(Math.random() * 100 + 1);
Copy after login

2. Randomly take out an item from an array

var array = ['a','b','c','d','e','f','g'];

/*array.length === 7,取0-6之间的随机整数*/
var index = Math.floor(Math.random() * 7 + 0);

/*随机取的结果是*/
var result = array[index];
console.log(result);
Copy after login

Related recommendations:

How to generate random numbers in PHP

js randomly generates 6 Bit random number

Summary of how PHP generates random numbers

The above is the detailed content of Detailed explanation of how to find random numbers in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!