js随机数

1、ID随机生成

当你执行原型制作并且需要唯一的ID时,此功能可以成为你的首选项。

const a = Math.random()。toString(36).substring(2); 
console.log(a)
---------------------------- 
72pklaoe38u

2、生成一个范围内的随机数

在很多情况下,我们需要在一个范围内生成一个随机数。该Math.random函数可以帮助我们生成一个随机数,然后将其转换为所需的范围。

max = 20
min = 10
var a = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(a)
-------------------------
17

 

posted @ 2022-01-02 23:16  程序员小明1024  阅读(255)  评论(0编辑  收藏  举报