summary:
This article explains how to use js to generate random numbers between n and m. The main purpose is to prepare for later generation of js verification codes.
The Math.random() function returns a pseudo-random number between 0 and 1, which may be 0, but is always less than 1, [0,1)
Generate nm, integers containing n but not m:
The first step is to calculate the value of mn, assuming it is equal to w
Step 2 Math.random()*w
Step 3 Math.random()*w+n
Step 4 parseInt(Math.random()*w+n, 10)
Generate nm, integers that do not contain n but contain m:
The first step is to calculate the value of mn, assuming it is equal to w
Step 2 Math.random()*w
Step 3 Math.random()*w+n
Step 4 Math.floor(Math.random()*w+n) + 1
Generate nm, integers that do not contain n and m:
The first step is to calculate the value of mn-2, assuming it is equal to w
Step 2 Math.random()*w
Step 3 Math.random()*w+n +1
Step 4 Math.round(Math.random()*w+n+1) or Math.ceil(Math.random()*w+n+1)
Generate nm, random numbers containing n and m:
The first step is to calculate the value of mn, assuming it is equal to w
Step 2 Math.random()*w
Step 3 Math.random()*w+n
Step 4 Math.round(Math.random()*w+n) or Math.ceil(Math.random()*w+n)
example:
Generate random integers of 800-1500, containing 800 but not 1500
The code copy is as follows:
1500-800 = 700
Math.random()*700
var num = Math.random()*700 + 800;
num = parseInt(num, 10);
It only takes four simple steps to complete.
Replenish:
Math.ceil() returns the smallest integer greater than or equal to the numeric parameter (rounding function), rounding the numeric number
Math.floor() returns the maximum integer less than or equal to the numeric parameter, rounding the number
Math.round() returns the integer closest to the number, rounded
PS: Here are two related online tools for your reference:
Online random number/string generation tool:
http://tools.VeVB.COM/aideddesign/suijishu
High-strength password generator:
http://tools.VeVB.COM/password/CreateStrongPassword