改变Fisher-Yates shuffle,这样一个值永远不会在它开始的同一个地方结束

问题描述

我有一个对象数组,其中一个示例如下所示:

var allWorm = [
        {
            "name": "Null","power": "Create an artificial cluster. Everyone added to this cluster has their parahuman abilities shared,but with a decrease in the power of each new ability.","fromCanon": "Null"
        },{
            "name": "One","power": "Thinker ability that allows for quick and efficient brainwashing given sufficient control over the victim's environment.","fromCanon": "One"
        },{
            "name": "Two","power": "Magnify other powers in close proximity.","fromCanon": "Two"
        },{
            "name": "Four","power": "Limited flight. Can hover between five and ten feet from the ground with a top speed of fifty to sixty miles per hour.","fromCanon": "Four"
        },{
            "name": "Nine","power": "Can push and pull on Metals within a short range,accelerating both self and object away from or towards each other.","fromCanon": "Nine"
        },{
            "name": "Thirteen","power": "Forcefield creation.","fromCanon": "Thirteen"
        }
]

在我的 HTML 中,我有一个复选框。如果选中该框,我可以使用 shuffle 函数导致条目被随机打乱,可能会在它开始的同一位置留下一个值。然而,我试图改变 else 语句中显示的算法导致出现问题,程序陷入无限循环。

function shuffle(){
    //see if Box checked for duplicates
    var samePower = document.getElementById("samePower");
    
    let allWorm = JSON.parse(localStorage.getItem("allWorm"));
    
    var currentIndex = allWorm.length,temporaryValue,randomIndex;
    
    if(samePower.checked == true){
        //while non-shuffled elements remain
        while(0 !== currentIndex){
            //pick remaining element
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
            
            //swap with current element
            temporaryValue = allWorm[currentIndex].power;
            allWorm[currentIndex].power = allWorm[randomIndex].power;
            allWorm[randomIndex].power = temporaryValue;
                
            temporaryValue = allWorm[currentIndex].fromCanon;
            allWorm[currentIndex].fromCanon = allWorm[randomIndex].fromCanon;
            allWorm[randomIndex].fromCanon = temporaryValue;
        }
    }
    else{
        //while non-shuffled elements remain
        while(0 !== currentIndex){
            //pick remaining element
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
            //disallow same power
            while(allWorm[randomIndex].name === allWorm[randomIndex].fromCanon){
                randomIndex = Math.floor(Math.random() * currentIndex);
                break;
            }
            
            //swap with current element
            temporaryValue = allWorm[currentIndex].power;
            allWorm[currentIndex].power = allWorm[randomIndex].power;
            allWorm[randomIndex].power = temporaryValue;
                
            temporaryValue = allWorm[currentIndex].fromCanon;
            allWorm[currentIndex].fromCanon = allWorm[randomIndex].fromCanon;
            allWorm[randomIndex].fromCanon = temporaryValue;
        }
    }
    
    var allWorm_serialized = JSON.stringify(allWorm);
    localStorage.setItem("allWorm",allWorm_serialized);
    writeData();
}

如何正确修改算法,?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)