装备炼级问题

背景:装备初始等级0级,每一块钱炼级一次,每次炼级都有一定概率成功,成功则等级+1,失败则等级-1,最多降级到0级不再继续降级;此外,装备油4级升至5级有个捷径,需要固定5次炼级;

计算装备升到9级平均需要炼级多少次,即需要充值多少元。

数学计算我忘了,毕竟概率论都是很多年前学的了,这里用js模拟跑一下:

注意:从0级到9级,逐级成功的概率为:[0.85,0.6,0.55,0.5,0.5,0.4,0.35,0.25,0.2]

let arr = [];
let sum = 0;
let average = 0;//升到9级平均次数
const cTimes = 10000;//实验次数
const pList = [0.85,0.6,0.55,0.5,0.5,0.4,0.35,0.25,0.2];
function getTimes(level=0,times=0){
    console.log('level:', level);
    console.log('times:', times);
    //level 0 1 2 3 4 5 6 7 8 9
    let _level = level;
    let _times = times;
    if(_level===9){
        arr.push(times);
        return _times;
    }else if(_level===4){
        getTimes(_level+1, _times+5);
    }else {
        while(_level === level){
            let result = Math.random();
            if(result<pList[_level]) {
                _level++;
            }else {
                if(_level>0) _level--;
            }
            _times++;
        }
        getTimes(_level, _times);
    }
}
function run(){
    for(let i =0;i<cTimes;i++) getTimes();
    console.log('arr:', arr);
    sum = arr.reduce((a, b) => (a + b));
    console.log('sum:', sum);
    average = sum/cTimes;
    console.log('average:', average);
}
run();

跑了一万次,平均需要充值392块钱;

posted @ 2021-01-19 14:40  我将枕中记忆抹去任岁月浮光掠影  阅读(75)  评论(0编辑  收藏  举报