【问题标题】:Picking array based off of percentage and shuffling it根据百分比选择数组并对其进行洗牌
【发布时间】:2022-01-22 12:56:49
【问题描述】:

我已经确定了我的百分比以及我的数组。我知道我需要这样做,以便百分比确定选择哪个数组,然后我需要洗牌该数组以使其吐出三个“事物”之一。我知道有一种更简单/更有效的方法可以做到这一点,而无需使用一百万个随机函数来确定“Thing”变量。

目前,它不起作用(吐出“未定义”),但它让我摸不着头脑,因为我不确定问题是什么,并且想要简化它。

代码的全部要点是根据滚动百分比选择一个数组,将该数组随机化,然后吐出它通过洗牌得到的值。

我正在使用的当前绝对垃圾箱火灾:

function generate(){

  var tierOne = ["thing one", "thing two", "thing three"]
  var tierTwo = ["thing four", "thing five", "thing six"]
  var tierThree = ["thing seven", "thing eight", "thing nine"]
  var tierFour = ["thing ten", "thing eleven", "thing twelve"]
  var tierFive = ["thing thirteen", "thing fourteen", "thing fifteen"]
  
    var percent = r();

    if (percent >= 0 && percent < 25) {
        shuffle(tierOne)
        thing = tierOne;
        return thing[0];
    } else if (percent >= 25 && percent < 36) {
        shuffle(tierTwo)
        thing = tierTwo;
        return thing[0];
    } else if (percent >= 36 && percent < 60) {
        shuffle(tierThree)
        thing = tierThree;
        return thing[0];
    } else if (percent >= 60 && percent < 76) {
        shuffle(tierFour)
        thing = tierFour;
        return thing[0];
    } else {
        shuffle(tierFive)
        thing = tierFive;
        return thing[0];
    }
} 

function r() {
    Math.floor(Math.random() * 100) + 1;
    return Math.floor(Math.random() * 100) + 1;
}```

【问题讨论】:

  • 你能解决什么问题吗?你想优化你的代码还是你当前的代码不起作用?
  • 进行编辑以澄清我的代码不起作用并且我希望对其进行优化。没有意识到我没有将其添加到我的原始帖子中,谢谢您指出! @丹尼尔克鲁兹
  • 洗牌并只取第一项就像在随机索引上挑选一样。

标签: javascript arrays generator shuffle weighted


【解决方案1】:

首先,您的函数r 有一个重复的行,因此您可以将其简化为:

function r() {
    return Math.floor(Math.random() * 100) + 1;
}

我看到你没有发布你的随机播放功能,所以我猜你已经定义了这个,或者你可以使用你可以在这里找到的一个:https://stackoverflow.com/a/12646864/17537072

最后,我会把它做成一个矩阵,这样你就可以有任意多的层。然后使用模数根据您拥有的层数计算正确的索引。代码结果如下:

function generate(){
    let tiers = [
      ["thing one", "thing two", "thing three"],
      ["thing four", "thing five", "thing six"],
      ["thing seven", "thing eight", "thing nine"],
      ["thing ten", "thing eleven", "thing twelve"],
      ["thing thirteen", "thing fourteen", "thing fifteen"]
    ];
  
    var percent = r();
    
    return shuffle(tiers[percent % tiers.length]);
} 

tiers.length 是层数,因此在您的示例中为 5。然后 % 生成 0 和 n-1 之间的任何数字,从而匹配数组的索引。

我认为这是简短而动态的。

【讨论】:

    【解决方案2】:

    首先,我认为没有必要设置不同的数组,然后使用 if-then-else 逻辑将百分比与一系列值进行比较。只需制作一个数组数组并使用索引返回要随机播放的数组。这也意味着除非你真的需要 1-100 的数字来做其他事情,那么你可能应该只生成一个 0 到 4 之间的随机数。假设你需要我留下的百分比并将其缩放到 0 到 4 之间.

    我可能也不会将 shuffle 逻辑与 generate 函数分开,但我将其分开,以便您可以更轻松地实现所需的 shuffle 逻辑。我不相信 js 中有一个内置的 shuffle 函数,就像在其他一些语言中那样,所以你必须有一个 shuffle 函数。这是一个关于洗牌的帖子,我无耻地偷走了我从中包含的洗牌功能。不是说这是最好的,只是看起来很漂亮。在那篇文章中有很多关于不同洗牌算法的不同含义的讨论。

    How to randomize (shuffle) a JavaScript array?

    console.log(generate());
    
    function generate(){
      
      const raw = [
        ["thing one", "thing two", "thing three"],
        ["thing four", "thing five", "thing six"],
        ["thing seven", "thing eight", "thing nine"],
        ["thing ten", "thing eleven", "thing twelve"],
        ["thing thirteen", "thing fourteen", "thing fifteen"]
      ];
      
      var percent = Math.floor(Math.random() * 100) + 1;
      var i = Math.ceil(percent/20)-1;
      return shuffle(raw[i])[0];
      
    }
    function shuffle(unshuffled){
    
      return unshuffled
        .map((value) => ({ value, sort: Math.random() }))
        .sort((a, b) => a.sort - b.sort)
        .map(({ value }) => value)
      ;
      
    }

    【讨论】:

      【解决方案3】:
      1. 您的 shuffle 例程可能应该返回一个带有结果的新数组。

      2. 您需要声明thing,并且不要使用全局变量

      if (percent >= 0 && percent < 20) {
              const thing = shuffle(tierOne)
              return thing[0];
          }
      

      let thing
      if (percent >= 0 && percent < 20) {
             thing = shuffle(tierOne)
              return thing[0];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-26
        • 1970-01-01
        相关资源
        最近更新 更多