1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| class GLRandom { /** * 构造函数 * @param {number} min 最小整数值 * @param {number} max 最大整数值 * @param {Map} percentage 概率数 [值,百分比] */ constructor(min, max, percentage = new Map()) { this.min = Math.trunc(min); this.max = Math.trunc(max); this.MATH_RANGE = 100; // 分成100份 this.percentage = percentage; this.range(); }
get percentage() { if (!this._percentage) { this._percentage = new Map(); } return this._percentage; }
/** * 分配比例 * @param {Map} map 设置 值-百分比 */ set percentage(map) { let result = Array.from(map.values()).reduce((p, v, a) => { return p - v; }, 1); for (let i = this.min; i < this.max; i++) { if (map.has(i)) { this.percentage.set(i, map.get(i)); } else { this.percentage.set(i, result / (this.max - this.min - map.size)); } } // 重新设置取值范围 this.range() }
/** * 根据比例生成取值范围 */ range() { let [start, random, keys] = [0, this.MATH_RANGE, Array.from(this.percentage.keys())]; for (let i = 0; i < keys.length; i++) { let temp = this.percentage.get(keys[i]); this.percentage.set(keys[i], [start, start += temp * random]); } }
/** * 生成随机数 */ create() { let num = Math.random() * this.MATH_RANGE; for (let data of this.percentage.entries()) { if (num >= data[1][0] && num < data[1][1]) { return data[0]; } } return null; } }
|