【问题标题】:Way to add specific % chances to certain ranges of randomly generated numbers 0-90将特定百分比机会添加到某些随机生成的数字范围 0-90 的方法
【发布时间】:2021-06-13 05:36:59
【问题描述】:

因此,我希望随机生成的数字在 0 到 20 或 75 到 90 之间的可能性较低,但该数字在当前随机代码之间的 20 到 75 之间的可能性更高。

我尝试了很多类型的东西,但我似乎无法弄清楚。

生成数字的数学

function doDamage(attacker, reciever, channel, attack) {

      let healthbothusers = 100

      const healing = testing === false ? Math.floor(healthbothusers = Math.random() * 80) : 100
      const damage = testing === false ? Math.floor(healthbothusers = Math.random() * 90) : 100

      let recieverHealth = usersHealth.get(reciever)
      let attackerHealth = usersHealth.get(attacker)

      let newHealth = recieverHealth - damage

      let beenhealed = attackerHealth + healing

这是我的第一个堆栈帖子,所以我希望它做得正确;)

【问题讨论】:

  • 好的,我去看看!
  • 我喜欢创建一系列可能的结果,然后从中随机抽取而不是凭空抽取。要称重,只需重复一些您希望更频繁出现的数组项,例如:从 [1,1,2,3] 拉出 1 的出现频率是 2 或 3 的两倍。
  • 嗯,当然,您的游戏机制取决于您和您正在尝试制作的游戏,但我认为通过基本上三个具有“硬”边界的均匀分布进行采样有点武断。所以 21、74 和 45 都有相同的可能性,但 20 和 75 则不然。似乎很奇怪。可以说,以 45 为中心的高斯分布会给你更多的自然伤害值。当然,您必须对其进行剪辑并绘制几个条形图以获得所需的方差。
  • 我考虑了一段时间的伤害值,但我也有治疗,所以这就是我最终使用这些值的原因

标签: javascript discord.js


【解决方案1】:

两次Math.random - 第一次确定您想要哪个结果块(翅膀,或更常见的中心范围),然后再次执行以计算该块内的随机结果。
因此,您可以控制机翼出现的频率。与自然分布相比,这将每个侧翼出现的机会减半:

let r=Math.random()
if (r<0.1) { // Giving 0-20 only a 10% chance of occurring
 r=Math.floor(Math.random() * 20)
} else if (r>0.92){  // Giving 75-90 only a 8% chance of occurring
 r=75+Math.floor(Math.random() * 15)
} else { //Giving central range the remainder chance (82%) 
 r=20+Math.floor(Math.random() * 55) //Range goes from 20 to 75
}

【讨论】:

    【解决方案2】:

    Math.floor(Math.random()*100)+1 应为您提供介于 1 和 100 之间的百分比。只要该随机百分比是 &gt; 100-yourPercent,它应该大约是正确的时间百分比。看看这个:

    //<![CDATA[
    /* js/external.js */
    let doc, htm, bod, nav, M, I, mobile, S, Q, Hopeful; // for reuse on other loads
    addEventListener('load', ()=>{
    doc = document; htm = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);
    mobile = nav.userAgent.match(/Mobi/i) ? true : false;
    S = (selector, within)=>{
      let w = within || doc;
      return w.querySelector(selector);
    }
    Q = (selector, within)=>{
      let w = within || doc;
      return w.querySelectorAll(selector);
    }
    Hopeful = function(percent){
      this.percent = percent;
      this.chanceIt = ()=>{
        const p = this.percent;
        return Math.floor(Math.random()*100)+1 > 100-this.percent;
      }
    }
    // small Library above for reuse - magic below can be put on another page using a load Event *(except // end load line and below)*
    const fifty = new Hopeful(50), twentyFive = new Hopeful(25), one = new Hopeful(1);
    const seventyFive = new Hopeful(75), oneHundred = new Hopeful(100), zero = new Hopeful(0);
    function test(){ // approximates showing proof of concept
      let zeroN = 0, fiftyN = 0, twentyFiveN = 0, oneN = 0, seventyFiveN = 0, oneHundredN = 0;
      for(let i=0,l=100; i<l; i++){
        if(zero.chanceIt())zeroN++;
        if(one.chanceIt())oneN++;
        if(twentyFive.chanceIt())twentyFiveN++;
        if(fifty.chanceIt())fiftyN++;
        if(seventyFive.chanceIt())seventyFiveN++;
        if(oneHundred.chanceIt())oneHundredN++;
      }
      console.clear();
      console.log('zero', zeroN); 
      console.log('one', oneN); 
      console.log('twentyFive', twentyFiveN);
      console.log('fifty', fiftyN);
      console.log('seventyFive', seventyFiveN);
      console.log('oneHundred', oneHundredN);
    }
    const chance = I('chance');
    test(); chance.onclick = test;
    }); // end load
    //]]>
    /* css/external.css */
    *{ /* font size may affect white space line breaks - set individually */
      box-sizing:border-box; font:0; padding:0; margin:0;
    }
    html,body,.full{
      width:100%; height:100%;
    }
    .full{
      background:#ccc; padding:7px;
    }
    button{
      cursor:pointer; width:100%; background:linear-gradient(#1b7bbb,#147); color:#fff; font:bold 28px Tahoma, Geneva, sans-serif; padding:5px 10px; border:1px solid #007; border-radius:10px;
    }
    <!DOCTYPE html>
    <html lang='en'>
      <head>
        <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1, user-scalable=no' />
        <title>Title Here</title>
        <link type='text/css' rel='stylesheet' href='css/external.css' />
        <script src='js/external.js'></script>
      </head>
    <body>
      <div class='full'>
        <button id='chance'>chance it</button>
      </div>
    </body>
    </html>

    这种设计允许你给任何东西一个百分比的机会。

    【讨论】:

    • 我不明白你在这里做了什么。我相信它会起作用,但在我看来,您已经解决了太多超出范围的问题,因此我迷失了与 OP 相关的内容以及解决其他问题的补充内容。
    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 2018-08-20
    • 2016-05-04
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多