简体   繁体   English

我如何保证选择加权元素?

[英]How can I guarantee pick a weighted element?

can somebody help me, I am trying to make a simple javascript game that let's the user pick a random element according to it's weight.有人可以帮我吗,我正在尝试制作一个简单的 javascript 游戏,让用户根据其重量选择一个随机元素。 The problem is, it's hard for me to get the lowest weighted element in the array so, I realize that why not do a max number of pick and after that reaching the max number you'll be guaranteed to get the lowest weighted element.问题是,我很难获得数组中权重最低的元素,所以,我意识到为什么不进行最大数量的选择,然后达到最大数量,您将保证获得最低权重元素。 (I' so sorry for my english, as it's not my first language). (我很抱歉我的英语,因为它不是我的第一语言)。 Also I'm a beginner in javascript.我也是 javascript 的初学者。 >///< >///<

Here's the code:这是代码:

var firstPrize = 
[
        "200k Yen Cash",
        "High End Computer Set"
]; //first prize with 0.5% chance of getting picked

var secondPrize = 
[
        "Xbox",
        "PS5",
        "Nintendo Switch"
]; //second prize with 25.3% chance of getting picked

var thirdPrize = 
[
        "10k Yen Cash",
        "10k Yen Amazon Voucher",
        "Airpods",
        "Customized Keyboard",
        "1-Yr Nitro Boost",
        "Shoes"
]; //third prize with 74.2% chance of getting picked

var randFirstPrize = firstPrize[Math.floor(Math.random() * firstPrize.length)]; // random pick

var randSecondPrize = secondPrize[Math.floor(Math.random() * secondPrize.length)]; // random pick

var randThirdPrize = thirdPrize[Math.floor(Math.random() * thirdPrize.length)]; // random pick

const prizes = 
[
        randFirstPrize,
        randSecondPrize,
        randThirdPrize
]; // prizes as variables

const weights =
[
        0.5,
        25.3,
        74.2
]; //prize percent chance

function userPrize(prizes, weights) {
    var i;
  
    for (i = 0; i < weights.length; i++)
    weights[i] += weights[i - 1] || 0;
    
    var random = Math.random() * weights[weights.length - 1];
    
    for (i = 0; i < weights.length; i++)
        if (weights[i] > random)
            break;
    
    return prizes[i];
}; //pick random weighted elements

console.log(userPrize(prizes, weights));

Expected Output will mostly be thirdPrize as the weight is higher than the other prizes.由于权重高于其他奖项,预计thirdPrize将主要是三等奖。 So my goal is, when a player reaches 150 pick/accumulation then he will be guaranteed to pick firstPrize .所以我的目标是,当玩家达到 150 次选择/累积时,他将保证选择firstPrize Hope I explained my question well.希望我能很好地解释我的问题。

I assume the code you posted currently work as intended and you want to add the function that "when a player reaches 150 pick/accumulation then he will be guaranteed to pick randFirstPrize".我假设您发布的代码当前按预期工作,并且您想添加 function “当玩家达到 150 次选择/累积时,他将保证选择 randFirstPrize”。

To do that, you will need a variable to keep track of how many times they picked:为此,您将需要一个变量来跟踪他们选择了多少次:

let pickTimes = 0;

And you need to add it every time a user pick:每次用户选择时,您都需要添加它:

    // ...
    pickTimes++;
    return prizes[i];

Now, obviously you can check the pickTimes variable every time you choose a random prize.现在,显然您可以在每次选择随机奖品时检查pickTimes变量。 It's up to you how you process it but this is one suggestion:这取决于您如何处理它,但这是一个建议:

    var random = Math.random() * weights[weights.length - 1];
    
    if (pickTimes == 150) {
        random = 100; // So your weight for first prize always meets the first prize condition
        pickTimes = 0; // You may want to reset the pick times so 150 times later users can still get guaranteed first prize.
    }

Ps after a more thorough reading, I do not understand your code of picking weight. ps经过更彻底的阅读,我不明白你的取重代码。 I may be wrong but you should understand the principle of how to apply it from the logic I explained above after fixing your code if there is a problem.我可能错了,但是如果出现问题,您应该在修复代码后从我上面解释的逻辑中了解如何应用它的原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM