js 随机生成整数

随机生成一个唯一的整数 id

export const randomId = () => {

  return Date.now() + Math.floor(Math.random() * 10000)

}

生成随机ID的方法

// 随机生成0 - 9999 

export const randomId = ()=> { return Math.floor(Math.random() * 10000).toString() }

// 随机生成0-999之间

  export const randomId = function () { return Math.floor(Math.random() * 1000).toString() }

console.log(randomId())

export const randomId = function () {

  return Math.floor(Math.random() * 10000) + ''

}

 使用当前时间的毫秒数 Date.now() 生成随机数

export const randomId = function () {
  const now = Date.now()
  return (now % 1000).toString() // 生成 0 到 999 之间的随机数
}

使用 crypto 模块生成随机数

export const randomId = function () {
  const array = new Uint32Array(1)
  window.crypto.getRandomValues(array)
  return (array[0] % 1000).toString() // 生成 0 到 999 之间的随机数
}
 

基于一个简单的伪随机数生成器(PRNG)

export const randomId = function () {
  let seed = new Date().getTime() % 1000 // 初始种子
  seed = (seed * 9301 + 49297) % 233280 // 伪随机数生成公式
  return (seed % 1000).toString() // 生成 0 到 999 之间的随机数
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值