如何在循环中记录连胜-制作赔率计算器

我正在制作一个赔率计算器。我试图打印出一个玩家可能获胜的最高连胜次数和平均连胜次数。

我所说的连胜指的是在对手获胜前,玩家连续获胜的次数,反之亦然。

我使用了40%与60%的胜率进行计算。

while (nFlip <= 99) do

    nFlip = nFlip + b

    if math.random(0,4) < 2 then
        countPlayer = countPlayer + b
--!!!!! 如何记录玩家的连胜和平均连胜次数
        print( "玩家获胜" )
    elseif math.random(0,4) < 3 then
        countHouse = countHouse + b
 --!!!!! 如何记录玩家的连胜和平均连胜次数
        print( "庄家获胜" )
    end

end
点赞
用户869951
用户869951

如果连胜的定义是连续胜利的次数,则计算该次数,并将其存储在一个表中。

local playerStreak = 0
local houseStreak = 0
local playerStreaks = {}
local houseStreaks = {}
local nFlip = 0

math.randomseed(os.time())

while nFlip <= 99 do
    nFlip = nFlip + 1

    local r = math.random()
    local playerWinProb = 0.4
    if r < playerWinProb then -- 玩家赢了!
        playerStreak = playerStreak + 1
        if houseStreak > 0 then -- 上一局庄家赢了,庄家连胜结束
            table.insert(houseStreaks, houseStreak)
            houseStreak = 0
        end
        -- print( "Player Wins" )
    else
        houseStreak = houseStreak + 1
        if playerStreak > 0 then -- 上一局玩家赢了,玩家连胜结束
            table.insert(playerStreaks, playerStreak)
            playerStreak = 0
        end
        -- print( "House Wins" )
    end
end

table.sort(playerStreaks)
print('最大玩家连胜次数:', playerStreaks[#playerStreaks])
table.sort(houseStreaks)
print('最大庄家连胜次数:', houseStreaks[#houseStreaks])

在所有抛硬币的次数之后,您可以计算统计数据:

  • 对于最大的玩家连胜次数,搜索 playerStreaks 中的最大值(我比较懒,使用了一个排序,虽然不是很高效但也不错);同样,对于最大的庄家连胜次数也是如此
  • 对于平均值,将所有玩家连胜次数相加,然后除以 #playerStreaks;庄家连胜次数同样处理
  • 等等。
2014-05-21 04:27:43