{"id":2271,"date":"2023-10-27T14:57:39","date_gmt":"2023-10-27T06:57:39","guid":{"rendered":"https:\/\/courtship.top\/?p=2271"},"modified":"2023-10-27T14:57:39","modified_gmt":"2023-10-27T06:57:39","slug":"%e5%9f%ba%e4%ba%8e%e6%9d%83%e9%87%8d%e7%9a%84%e9%9a%8f%e6%9c%ba%e9%80%89%e6%8b%a9%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2023\/10\/27\/%e5%9f%ba%e4%ba%8e%e6%9d%83%e9%87%8d%e7%9a%84%e9%9a%8f%e6%9c%ba%e9%80%89%e6%8b%a9%e7%ae%97%e6%b3%95\/","title":{"rendered":"\u57fa\u4e8e\u6743\u91cd\u7684\u968f\u673a\u9009\u62e9\u7b97\u6cd5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u77e5\u4e4e<\/p>\n<cite>\u57fa\u4e8e\u6743\u91cd \u7684\u968f\u673a\u9009\u62e9\u7b97\u6cd5 - co lin\u7684\u6587\u7ae0 - \u77e5\u4e4e https:\/\/zhuanlan.zhihu.com\/p\/146216606<\/cite><\/blockquote>\n\n\n\n<p>\u6e38\u620f\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u78b0\u5230\u8fd9\u6837\u7684\u9700\u6c42\uff1a\u4ece\u4e00\u7cfb\u5217\u9053\u5177\u4e2d\u968f\u673a\u62bd\u51fa\u4e00\u4e2a\u53d1\u7ed9\u73a9\u5bb6\uff0c\u7531\u4e8e\u9053\u5177\u7684\u4ef7\u503c\u4e0d\u4e00\u6837\uff0c\u6240\u4ee5\u62bd\u53d6\u7684\u6982\u7387\u5e76\u4e0d\u662f\u5e73\u5747\u7684\u3002\u540e\u53f0\u4f1a\u5904\u7406\u6210\u9ad8\u4ef7\u503c\u7684\u4e1c\u897f\u5f88\u96be\u5f97\u5230\uff0c\u8981\u5b9e\u73b0\u8fd9\u79cd\u6548\u679c\uff0c\u9700\u8981\u7ed9\u6bcf\u4e2a\u9053\u5177\u6307\u5b9a\u4e00\u4e2a\u6743\u91cd\u3002\u6bd4\u5982\u9053\u5177A\u7684\u6743\u91cd\u662f99\uff0c\u9053\u5177B\u7684\u6743\u91cd\u662f1\uff0c\u90a3\u4e4899%\u7684\u6982\u7387\u4f1a\u62bd\u5230\u9053\u5177A\uff0c\u8fd9\u5c31\u662f\u57fa\u4e8e\u6743\u91cd\u7684\u968f\u673a\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u6309\u6743\u91cd\u968f\u673a\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u7ebf\u6027\u626b\u63cf\uff0c\u5927\u6982\u8fc7\u7a0b\u662f\u5148\u8ba1\u7b97\u51fa\u6240\u6709\u9053\u5177\u7684\u6743\u91cd\u603b\u548cS\uff0c\u7136\u540e\u8c03\u7528\u968f\u673a\u51fd\u6570\u5f97\u5230\u4e00\u4e2a\u533a\u95f4\u5728[0, S)\u7684\u968f\u673a\u503c\uff0c\u63a5\u7740\u4ece\u5934\u5411\u540e\u626b\u63cf\u9053\u5177\u5217\u8868\uff0c\u5e76\u4e0d\u65ad\u4eceS\u51cf\u6389\u6bcf\u4e2a\u9053\u5177\u7684\u6743\u91cd\u503c\uff0c\u5f53S\u5c0f\u4e8e\u67d0\u4e2a\u9053\u5177\u7684\u6743\u91cd\u503c\u65f6\uff0c\u8fd9\u4e2a\u9053\u5177\u5c31\u662f\u62bd\u4e2d\u7684\u90a3\u4e00\u4e2a\u3002\u903b\u8f91\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u7ebf\u6027\u626b\u63cf\nlocal function prepare_weighted_random1(values, weights)\n    assert(#values == #weights)\n    local sum = 0       -- \u8ba1\u7b97\u603b\u6743\u91cd\n    for _, wt in ipairs(weights) do\n        sum = sum + wt\n    end\n    return function()\n        local n = math.random(1, sum)       -- \u7ebf\u6027\u626b\u63cf\n        for idx, wt in ipairs(weights) do\n            if n &lt;= wt then\n                return values&#91;idx], weights&#91;idx]\n            end\n            n = n - wt\n        end\n    end\nend<\/code><\/pre>\n\n\n\n<p>\u5f15\u7528\u9879\u76ee\u7684\u5b9e\u9645\u9700\u6c42<\/p>\n\n\n\n<p><strong>\u53ef\u4ee5\u88ab\u968f\u673a\u5230\u7684\u6743\u91cd<\/strong>\u3002\u7528\u4e8e\u63a7\u5236\u52a8\u4f5c\u8d44\u6e90\u88ab\u968f\u673a\u5230\u7684\u6982\u7387\u3002\u5f53\u524d\u671f\u671b\u52a8\u4f5c\u5e45\u5ea6\u5927\u7684\u6743\u91cd\u5c0f\uff0c\u88ab\u968f\u673a\u5230\u7684\u6982\u7387\u5c0f\uff1b\u52a8\u4f5c\u5e45\u5ea6\u5c0f\u7684\u6743\u91cd\u5927\uff0c\u88ab\u968f\u673a\u5230\u7684\u6982\u7387\u5927\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FBlendSpaceParam UInworldCharacterAnimationsLib::WeightedRandom(TArray&lt;FBlendSpaceParam>&amp; RandomPool, TArray&lt;FBlendSpaceParam>&amp; BlendSpaces)\n{\n    if (RandomPool.Num() == 0 || BlendSpaces.Num() == 0)\n    {\n        return FBlendSpaceParam();\n    }\n\n    int32 TotalWeight = 0;\n    for (const auto&amp; bl : RandomPool)\n    {\n        TotalWeight += bl.Weight;\n    }\n\n    int32 RandomNumber = FMath::RandRange(0,TotalWeight - 1);\n\n    float WeightSum = 0.0f;\n\n    RandomPool.Sort(CompareWeight);\n\n    for (int i = 0;i &lt; RandomPool.Num();i++)\n    {\n        WeightSum += RandomPool&#91;i].Weight;\n        if (WeightSum >= RandomNumber)\n        {\n            for (int j = 0;j &lt; BlendSpaces.Num();j++)\n            {\n                if (BlendSpaces&#91;j] == RandomPool&#91;i])\n                {\n                    BlendSpaces&#91;j].MaxTimes--;\n                    \/*if (BlendSpaces&#91;j].MaxTimes == 0)\n                    {\n                        BlendSpaces.RemoveAt(j);\n                    }*\/\n                }\n            }\n            return RandomPool&#91;i];\n        }\n    }\n\n    return RandomPool&#91;0];\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u77e5\u4e4e \u57fa\u4e8e\u6743\u91cd \u7684\u968f\u673a\u9009\u62e9\u7b97\u6cd5 &#8211; co lin\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e https:\/\/zhuanlan.zhihu.com\/p\/146 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7,17],"tags":[],"class_list":["post-2271","post","type-post","status-publish","format-standard","hentry","category-games","category-17"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2271"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2271\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}