Probability of random number controlled by Js

Such as:

Take the random number between 1 and 10, then their value range is:

 

integer

Section

probability

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

 

If the probability of adjustment 2 is 0.5, then the probability of the random number value interval between 1 and 10 and other values will change as follows:

Minimum value range = 0

Maximum value range = minimum value range + probability * 10 (divided into 10 parts)

 

integer

Section

probability

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

 

 

Then the random function is called to generate the random number between 1~10. If the generated random number is within the range of an integer, then the current integer is output.

 

/*
 * @Author: Tadpole from broken shell
 * @Blog: 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 {
    /**
     * Constructor
     * @param {number} min  Minimum integer value
     * @param {number} max  Maximum integer value
     * @param {Map} percentage Probability number [value, percentage]
     */
    constructor(min, max, percentage = new Map()) {
        this.min = Math.trunc(min);
        this.max = Math.trunc(max);
        this.MATH_RANGE = 100;  // Divided into 100 parts.
        this.percentage = percentage;
    }

    get percentage() {
        if (!this._percentage) {
            this._percentage = new Map();
        }
        return this._percentage;
    }

    /**
     * Distribution ratio
     * @param {Map} map Set value percentage
     */
    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));
            }
        }
    }

    /**
     * Generate value range according to proportion
     */
    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]);
        }
    }

    /**
     * Generate random number
     */
    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;
    }
}

 

 

// sample
{
    // Random number range: 40 ~ 900 
    let random = new GLRandom(40, 100);

    // Adjustment probability
    random.percentage = new Map([
        [41,0.2],   // A value with an adjusted value of 41 and a probability of 20%  
        [46,0.5],   // A value with an adjusted value of 46 and a probability of 50%  
        [90,0.05]   // A value with an adjusted value of 90 and a probability of 5%  
    ]);

    // Generating value interval
    random.range();

    // Generating probability random number
    console.log(random.create());
}

Baidu experience address: https://jingyan.baidu.com/article/5553fa827bfe7d65a239341d.html

Source address: https://pan.baidu.com/s/1ieohaye34naha8jfhfzaw

Tags: Javascript shell

Posted on Tue, 03 Dec 2019 11:35:15 -0500 by dmikester1