100人の男性から37人を選び、その中で最高の男性の確率はどれくらいですか?

-- 随机种子. 根据当前时间来获取随机
math.randomseed(os.time())


-- 男人数量
local nManCount = 100;
-- 挑选数量
local nPick = 37; 
-- 随机次数
local nTotalCount = 1000000


-- 男人列表
local manList = {};




-- 先随机100个男人
for i=1,nManCount do
    table.insert(manList, i)
end;




local function pick()
    -- 100个男人进行洗牌
    for i=1, nManCount do
        local r = math.random(nManCount)
        manList[i], manList[r] = manList[r], manList[i]
    end;

    local nBig = 0;

    -- 洗牌结束, 进行挑选
    for i=1, nPick do
        if manList[i] == 100 then
            -- print("挑到了最优秀的男人")
            return 100
        end;

        if manList[i] > nBig then
            nBig = manList[i]
        end
    end;

    return nBig
end;



-- 结果
local  result = {}

for i=1, nTotalCount do
    local nValue = pick()
    result[nValue] = (result[nValue] or 0) + 1
end;


for k,v in pairs(result) do
    print(k,v)
end

Tsinghuaのボスの要件に従って書かれたコード。

答えを得た

1 74
75 1
78 4
79 9
80 15
81 29
82 32
83 72
84 123
85 212
86 364
87 638
88 1090
89 1859
90 3009
91 5113
92 8354
93 13511
94 22264
95 36632

96 58495
97 93681
98 148802
99 235773
100 369917
[ 9.4秒で終了]

おすすめ

転載: blog.csdn.net/warrially/article/details/103323881