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()*(最大值-最小值)+最小值)
原文地址:https://www.cnblogs.com/angfl/p/random_floor_round.html