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

时间:2014-05-20 04:10:59

标签: math lua corona

我正在制作赔率计算器。我试图打出一个玩家将获胜的最高连胜,并且平均连胜。

当我提到连胜时,我的意思是在对手获胜1之前,球员连续多少次获胜,反之亦然。

我的赔率为40%vs 60%。

while (nFlip <= 99) do

    nFlip = nFlip + b 

    if math.random(0,4) < 2 then
        countPlayer = countPlayer + b
--!!!!! How to record player streak and player average
        print( "Player Wins" )
    elseif math.random(0,4) < 3 then
        countHouse = countHouse + b
 --!!!!! How to record player streak and player average
        print( "House Wins" )
    end

end

1 个答案:

答案 0 :(得分:0)

如果条纹的定义是连续获胜的数量,那么计算它,并存储在表格中。

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 -- player wins!
        playerStreak = playerStreak + 1
        if houseStreak > 0 then -- house won last, end of its streak
            table.insert(houseStreaks, houseStreak)
            houseStreak = 0
        end
        -- print( "Player Wins" )
    else
        houseStreak = houseStreak + 1
        if playerStreak > 0 then -- house won last, end of its streak
            table.insert(playerStreaks, playerStreak)
            playerStreak = 0
        end
        -- print( "House Wins" )
    end
end

table.sort(playerStreaks)
print('largest player streak:', playerStreaks[#playerStreaks])
table.sort(houseStreaks)
print('largest house streak:', houseStreaks[#houseStreaks])

在所有翻转之后,您可以计算统计数据:

  • 对于最大的玩家连胜,搜索playerStreaks获取最大值(我很懒惰并且使用了排序,而不是效率但也不差);同样对于最大的房子连胜
  • 平均值,总计所有玩家条纹并除以#playerStreaks;同样适用于连胜
  • 等。