Home  >  Article  >  Backend Development  >  Detailed explanation of how to find random numbers in js

Detailed explanation of how to find random numbers in js

小云云
小云云Original
2018-03-31 16:39:132404browse

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() * 可能值的总数 + 第一个可能的值)

For example:

1. Find a random integer within 1-100

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

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);

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!

Statement:
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