useRandom
提供一些生成随机数据的方法。
randomNumber
在指定范围内生成一个随机整数。
- Supported Platforms:
Browser | Electron | App | Server | Scriptlet |
- Type Declarations:
ts
declare function randomNumber(
/**
* 最小值
* @default 0
*/
min?: number,
/**
* 最大值
* @default 100
*/
max?: number,
/**
* 四舍五入类型
* @default 'round'
*/
roundingType?: 'round' | 'ceil' | 'floor'
): number
- Example:
ts
import { useRandom } from '@lunarxyz/core'
const { randomNumber } = useRandom()
const randomNum = randomNumber(10, 20)
randomString
生成一个指定长度的随机字符串。
- Supported Platforms:
Browser | Electron | App | Server | Scriptlet |
- Type Declarations:
ts
declare function randomString(
/**
* 字符串长度
* @default 10
*/
length?: number
): string
- Example:
ts
import { useRandom } from '@lunarxyz/core'
const { randomString } = useRandom()
const randomStr = randomString(20)
randomBoolean
随机返回一个 Truly 或 Falsy 值。
- Supported Platforms:
Browser | Electron | App | Server | Scriptlet |
- Type Declarations:
ts
declare function randomBoolean(): boolean
- Example:
ts
import { useRandom } from '@lunarxyz/core'
const { randomBoolean } = useRandom()
const randomBool = randomBoolean()
shuffle
洗牌函数,用于对数组进行随机排序(支持 JSON 数组)。
- Supported Platforms:
Browser | Electron | App | Server | Scriptlet |
- Type Declarations:
ts
declare function shuffle(arr: any[]): any[]
- Example:
ts
import { useRandom } from '@lunarxyz/core'
const { shuffle } = useRandom()
const arr = [1, 2, 3, 4, 5, 6]
console.log(shuffle(arr))
// [4, 6, 5, 1, 3, 2]