local STATE = { cal = { hitCount = 10, missCount = 5, recent = {1,0,1,1} } } local function RecentRate() local Cal = STATE.cal if not Cal then return 0, 0, 0 end local Hits = Cal.hitCount or 0 local Total = Hits + (Cal.missCount or 0) local Recent = Cal.recent or {} local Sum = 0 for _, Value in ipairs(Recent) do Sum += Value end local Percent = #Recent > 0 and math.floor(Sum / #Recent * 100) or 0 return Hits, Total, Percent end local Hits, Total, Percent = RecentRate() print(Hits, Total, Percent) print(string.format("hit rate %d/%d recent %d%%", Hits, Total, Percent))