Js 控制隨機數概率


(新)控制隨機數概率:https://www.cnblogs.com/whnba/p/10565045.html 算法精簡了一下

 

 

如:

1~10 之間的隨機數,那么他們的取值范圍是

 

整數

區間

概率

1

[0,1)

0.1

2

[1,2)

0.1

3

[2,3)

0.1

4

[3,4)

0.1

5

[4,5)

0.1

6

[5,6)

0.1

7

[6,7)

0.1

8

[7,8)

0.1

9

[8,9)

0.1

10

[9,10)

0.1

 

如果調整2的概率為0.5,那么1~10之間的隨機數取值區間與其它值的概率都會發現變化 如下:

最小取值范圍 = 0

最大取值范圍 = 最小取值范圍  + 概率 * 10(分成10份)

 

整數

區間

概率

1

[0,0.55)

≈ 0.055

2

[0.55,5)

0.5

3

[5,5.55)

≈ 0.055

4

...

≈ 0.055

5

...

≈ 0.055

6

...

≈ 0.055

7

...

≈ 0.055

8

...

≈ 0.055

9

...

≈ 0.055

10

...

≈ 0.055

 

 

然后調用隨機函數生成1~10之間的隨機數,如果生成的隨機數在某個整數的取值范圍內,那么就輸出當前整數。

 

/*
 * @Author: 破殼而出的蝌蚪
 * @博客:https://www.cnblogs.com/whnba/ 
 * @Date: 2019-01-03 15:03:33 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2019-01-03 15:04:17
 */

'use strict';

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;
    }

    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));
            }
        }
    }

    /**
     * 根據比例生成取值范圍
     */
    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;
    }
}

 

 

// 樣本
{
    // 隨機數范圍 :40~900 
    let random = new GLRandom(40, 100);

    // 調整概率
    random.percentage = new Map([
        [41,0.2],   // 調整值為41的數值,概率為20%  
        [46,0.5],   // 調整值為46的數值,概率為50%  
        [90,0.05]   // 調整值為90的數值,概率為5%  
    ]);

    // 生成值區間
    random.range();

    // 生成概率隨機數
    console.log(random.create());
}

百度經驗地址:https://jingyan.baidu.com/article/5553fa827bfe7d65a239341d.html

源碼地址:https://pan.baidu.com/s/1ieOhaYe34nAhA8jfHhFzaw


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM