js生成随机数

将下列数组随机打乱

    let ary = [12, 2, 3, 2, 99, 32];
ary.sort((a, b) => {
    return Math.round(Math.random() * 10 - 5);
})

js常用取整方法:

  • Math.round : 取最近整数
  • Math.floor:向下取整
    js获取随机数
  • Math.random
    常用获取随机数规则(包含最大和最小值)
    Math.round(Math.random()*(最大值-最小值)+最小值)
posted @ 2019-08-27 10:16  ✔️zhangfl_go  阅读(419)  评论(0编辑  收藏  举报