【问题标题】:Is there a way to cast a a set of unequal line segments to enum without conditional constructions?有没有办法在没有条件结构的情况下将一组不相等的线段投射到枚举中?
【发布时间】:2021-08-06 17:37:24
【问题描述】:

所以,基本上,我已经实现了一个天真的稀有系统,我在其中随机生成了字节值(0-255)。从他们那里我确定了一个枚举变量:

                case < 100:
                return 0;
            case > 100 and < 180:
                return 1;
                case >180 and <235:
                    return 2;
            case > 235:
                return 3;

所以现在我到了我想改进我的一些代码的地方,而这种方法一直困扰着我。如您所见,对应于不同枚举数的值是不相等的。我开始想,有没有办法用公式来表示这种演员表?我有点坐立不安,但我真的不是数学家)所以我很感激一些帮助

【问题讨论】:

  • 这不是问题的答案,但我想你忘记了 = 的情况,这里没有 100、180 和 235。

标签: algorithm function enums functional-programming


【解决方案1】:

如果您想要这些精确的稀有度阈值,那么我会写类似rarity1 的内容。

如果你有点灵活,你可以使用rarity2rarity3。对于rarity2,本质上,我们将b 解释为定点分数x ∈ [0, 1) 并返回⌊4x²⌋。边界为 x = 1/2,x = 1/√2,x = √3/2,即b = 128b = 182b = 222

rarity3 选项不是单调的。它取高位和低位 2 位量的最小值。结果概率为 7/16、5/16、3/16、1/16,对应于决策边界 112、192、240。

def rarity1(b):
    if b < 100:
        return 0
    elif b < 180:
        return 1
    elif b < 235:
        return 2
    else:
        return 3


def rarity2(b):
    return (b * b) >> 14


def rarity3(b):
    return min(b >> 6, b & 3)

【讨论】:

    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多