Skip to content

随机数

randomDate

随机日期函数

typescript
/**
 * 随机日期函数
 * @param { string } start 开始日期 默认 '1999/01/1
 * @param { string } [end] 截止日期
 * @returns
 */
export function randomDate(start: string = '1999/01/1', end?: string) {
  const date = new Date()
  const splitTag = start.includes('/') ? '/' : '-'
  const [endY, endM, endD] = (end || '').split(splitTag)
  const y = +endY || date.getFullYear()
  const m = +endM || date.getMonth() + 1
  const d = +endD || date.getDate()
  const [startY, startM, startD] = start.split('/')
  const startDate = new Date(+startY, +startM, +startD).getTime()
  const endDate = new Date(y, m, d).getTime()
  return new Date(startDate + Math.round(Math.random() * (endDate - startDate)))
}

randomDate()

randomHexColor

随机 hex 颜色

typescript
/**
 * 随机hex颜色
 * @returns
 */
export function randomHexColor(): string {
  return `#${(Math.random() * 0xfffff * 1000000).toString(16).slice(0, 6)}`
}

randomHexColor()

randomRange

获取随机范围值

typescript
/**
 * 获取随机范围值
 * @param min 最小值 默认 MIN_SAFE_INTEGER
 * @param max 最大值 默认 MAX_VALUE
 * @returns
 */
export function randomRange(min: number = Number.MIN_SAFE_INTEGER, max: number = Number.MAX_VALUE) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

randomRange()

randomRgba

随机 rgba 颜色

typescript
/**
 * 随机rgba颜色
 * @param { number } [opacity] 透明度 默认 1
 * @returns
 */
export function randomRgba(opacity: number = 1) {
  return `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, ${opacity})`
}

randomRgba()

randomStr

随机字符串

typescript
const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
/**
 * 随机字符串
 * @param { number } size 长度 默认 16
 * @param { string } dict 从这些字符串中随机
 * @returns
 */
export function randomStr(size: number = 16, dict: string = urlAlphabet) {
  let id = ''
  let i = size
  const len = dict.length
  while (i--) id += dict[(Math.random() * len) | 0]
  return id
}

randomStr()

uuid

生成 uuid

typescript
import { isNum } from '../is/isNum'
/**
 * 生成uuid
 * @param { number } len 长度
 * @param {  number | 'hex' } radix  number | 'hex'
 * @returns
 */
export function uuid(len?: number, radix?: number | 'hex') {
  function isNum(o: any): o is number {
    return typeof o === 'number'
  }

  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  const uuid: string[] = []
  radix = radix || chars.length
  if (len) {
    const num = isNum(radix) ? (radix as number) : 36
    for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * num)]
  } else {
    if (len) {
      for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * (chars.length as number))]
    }

    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (let i = 0; i < 36; i++) {
      if (!uuid[i]) {
        const r = 0 | (Math.random() * 16)
        uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}

uuid()

randomNumber

随机数字,取值范围[min, max]。 min 默认 0,max 默认 1000

typescript
/**
 * 随机数字
 * @param min 最小值 默认0
 * @param max 最大值 默认1000
 * @returns {number} 随机数字,取值范围[min, max]
 */
export function randomNumber(min = 0, max = 1000): number {
  return Math.ceil(min + Math.random() * (max - min))
}

randomNumber()

Released under the MIT License.