Files
oops-plugin-framework/assets/core/common/random/RandomManager.ts

153 lines
5.4 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.
/** 引擎 utils.ts 中有一些基础数学方法 */
/** 随机管理 */
export class RandomManager {
private static _instance: RandomManager;
/** 是否运行在客户端环境 */
isClient: boolean = true;
/** 是否为全局伪随机 */
isGlobal: boolean = false;
private random: any = null;
/** 随机数管理单例对象 */
static get instance(): RandomManager {
if (this._instance == null) {
this._instance = new RandomManager();
this._instance.random = Math.random;
}
return this._instance;
}
private getRandom(): number {
return this.isGlobal ? Math.random() : this.random();
}
/** 设置随机种子 */
setSeed(seed: number) {
if (this.isClient) {
//注seedrandom.min.js文件在Cocos Creator中导入为插件生效
//@ts-ignore
if (Math.seedrandom) {
if (this.isGlobal)
//@ts-ignore
new Math.seedrandom(seed, { global: true });
else
//@ts-ignore
this.random = new Math.seedrandom(seed);
}
}
else {
var seedrandom = require('seedrandom');
if (this.isGlobal)
new seedrandom(seed, { global: true });
else
this.random = new seedrandom(seed);
}
}
/**
* 生成指定范围的随机浮点数
* @param min 最小值
* @param max 最大值
* @param type 类型
*/
getRandomFloat(min: number = 0, max: number = 1): number {
return this.getRandom() * (max - min) + min;
}
/**
* 生成指定范围的随机整数
* @param min 最小值
* @param max 最大值
* @param type 类型
* @example
var min = 1;
var max = 10;
// [min,max) 得到一个两数之间的随机整数,这个值不小于min如果min不是整数的话得到一个向上取整的 min并且小于但不等于max
RandomManager.instance.getRandomInt(min, max, 1);
// [min,max] 得到一个两数之间的随机整数,包括两个数在内,这个值比min大如果min不是整数那就不小于比min大的整数但小于但不等于max
RandomManager.instance.getRandomInt(min, max, 2);
// (min,max) 得到一个两数之间的随机整数
RandomManager.instance.getRandomInt(min, max, 3);
*/
getRandomInt(min: number, max: number, type: number = 2): number {
min = Math.ceil(min);
max = Math.floor(max);
switch (type) {
case 1: // [min,max) 得到一个两数之间的随机整数,这个值不小于min如果min不是整数的话得到一个向上取整的 min并且小于但不等于max
return Math.floor(this.getRandom() * (max - min)) + min;
case 2: // [min,max] 得到一个两数之间的随机整数,包括两个数在内,这个值比min大如果min不是整数那就不小于比min大的整数但小于但不等于max
return Math.floor(this.getRandom() * (max - min + 1)) + min;
case 3: // (min,max) 得到一个两数之间的随机整数
return Math.floor(this.getRandom() * (max - min - 1)) + min + 1;
}
return 0;
}
/**
* 根据最大值,最小值范围生成随机数数组
* @param min 最小值
* @param max 最大值
* @param n 随机个数
* @param type 类型
* @example
var a = RandomManager.instance.getRandomByMinMaxList(50, 100, 5)
console.log("随机的数字", a);
*/
getRandomByMinMaxList(min: number, max: number, n: number): Array<number> {
var result: Array<number> = [];
for (let i = 0; i < n; i++) {
result.push(this.getRandomInt(min, max))
}
return result;
}
/**
* 获取数组中随机对象
* @param objects 对象数组
* @param n 随机个数
* @example
var b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var r = RandomManager.instance.getRandomByObjectList(b, 5);
console.log("原始的对象", b);
console.log("随机的对象", r);
*/
getRandomByObjectList<T>(objects: Array<T>, n: number): Array<T> {
var temp: Array<T> = objects.slice();
var result: Array<T> = [];
for (let i = 0; i < n; i++) {
let index = this.getRandomInt(0, objects.length, n);
result.push(temp.splice(index, 1)[0]);
}
return result;
}
/**
* 定和随机分配
* @param n 随机数量
* @param sum 随机元素合
* @example
var c = RandomManager.instance.getRandomBySumList(5, -100);
console.log("定和随机分配", c);
*/
getRandomBySumList(n: number, sum: number): number[] {
var residue = sum;
var value = 0;
var result: Array<number> = [];
for (let i = 0; i < n; i++) {
value = this.getRandomInt(0, residue, 3);
if (i == n - 1) {
value = residue;
}
else {
residue -= value;
}
result.push(value);
}
return result;
}
}