First, let's write a function to get the label of the next chance based on the input chances table and the random probability.
function getNextChance(chances)
local r = math.random()
local cumulative = 0
for i, chance in ipairs(chances) do
cumulative = cumulative + chance.value
if r <= cumulative then
return chance
end
end
end
Now, let's simulate the process for a day and count the total earnings, jobs, and chances received.
local SECONDS_IN_HOUR = 3600
local HOURS_IN_DAY = 24
local TOTAL_SECONDS_IN_DAY = SECONDS_IN_HOUR * HOURS_IN_DAY
local totalEarnings = 0
local totalJobs = 0
local totalTime = 0
local totalChancesReceived = 0
local timeTaken = math.random(30, 60) + 7
while totalTime < TOTAL_SECONDS_IN_DAY do
local chance = getNextChance(chances)
totalEarnings = totalEarnings + chance.avp
totalJobs = totalJobs + 1
totalChancesReceived = totalChancesReceived + chance.value
totalTime = totalTime + timeTaken
timeTaken = math.random(30, 60) + 7 -- refresh the time between chances
end
local hourlyEarnings = totalEarnings / HOURS_IN_DAY
local jobsPerHour = totalJobs / HOURS_IN_DAY
local chancesPerHour = totalChancesReceived / HOURS_IN_DAY
Now let's loop through the possible chance values and simulate the process to get close to the required hourly earnings, considering the boundaries of 35,000 and 50,000.
local adjustedChances = chances
local requiredHourly = 40000
local minHourly = 35000
local maxHourly = 50000
local delta = 0.0001
repeat
-- Adjust the chances
for i, chance in ipairs(adjustedChances) do
chance.value = chance.value + delta
if chance.value > 1 then
chance.value = 1
end
end
-- Simulate the process for a day
local newHourlyEarnings = 0
local newTotalEarnings = 0
local newTotalJobs = 0
local newTotalTime = 0
local newTotalChancesReceived = 0
local newTimeTaken = math.random(30, 60) + 7
while newTotalTime < TOTAL_SECONDS_IN_DAY do
local chance = getNextChance(adjustedChances)
newTotalEarnings = newTotalEarnings + chance.avp
newTotalJobs = newTotalJobs + 1
newTotalChancesReceived = newTotalChancesReceived + chance.value
newTotalTime = newTotalTime + newTimeTaken
newTimeTaken = math.random(30, 60) + 7 -- refresh the time between chances
end
newHourlyEarnings = newTotalEarnings / HOURS_IN_DAY
until newHourlyEarnings >= minHourly and newHourlyEarnings <= maxHourly
Then, print the results.
print("Hourly earnings:", hourlyEarnings)
print("Jobs per hour:", jobsPerHour)
print("Chances per hour:", chancesPerHour)
print("Adjusted hourly earnings:", newHourlyEarnings)
Combine all the code above and run it to get the output showing the hourly earnings, jobs per hour, chances per hour, and the adjusted hourly earnings.