Files
jdt-fish-client/assets/FishSingle/script/engine/utils/RandomUtil.ts
2024-10-05 04:22:34 +08:00

47 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Vec2 } from 'cc'
export default class RandomUtil {
// 随机minNum到maxNum的数字 包含maxNum
public static nextInt(minNum: number, maxNum: number) {
return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum)
}
public static nextNumber(minNum: number, maxNum: number) {
return Math.random() * (maxNum - minNum) + minNum
}
public static nextSign() {
const temp = Math.random()
if (temp < 0.5) return 1
return -1
}
public static nextBoolean() {
const temp = Math.random()
return temp < 0.5
}
public static randomArr(nowArr: Array<any>, needNum: number) {
const tempArr: Array<any> = nowArr.concat()
const resultArr: Array<any> = []
for (let index = 0; index < needNum; index++) {
if (tempArr.length <= 0) break
const randomIndex: number = RandomUtil.nextInt(0, tempArr.length - 1)
resultArr.push(tempArr.splice(randomIndex, 1)[0])
}
return resultArr
}
public static randomItem(nowArr: Array<any>) {
return this.randomArr(nowArr, 1)[0]
}
public static randomP(left: number, right: number, up: number, down: number) {
const randomX: number = RandomUtil.nextNumber(left, right)
const randomY: number = RandomUtil.nextNumber(up, down)
return new Vec2(randomX, randomY)
}
}