Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

lua I've got this table local chances = { {value = 0.7992}, {value = 0.1598}, {value =…

  1. User

    lua

    I've got this table local chances = { {value = 0.7992}, {value = 0.1598}, {value = 0.0320}, {value = 0.0064}, {value = 0.0026} }

    it adds to 1

    write a function that changes it slightly but ensures that the total does not go over 1

  2. Assistant

    function adjustChances() local total = 0 for i, chance in ipairs(chances) do total = total + chance.value end if total == 1 then return chances elseif total > 1 then local excess = total - 1 for i = #chances, 1, -1 do if excess > 0 then local reduction = math.min(excess, chances[i].value) chances[i].value = chances[i].value - reduction excess = excess - reduction end end else local deficit = 1 - total while deficit > 0 do local index = math.random(#chances) if chances[index].value < 1 then local increase = math.min(deficit, 1 - chances[index].value) chances[index].value = chances[index].value + increase deficit = deficit - increase end end end return chances end