Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local numCount = 4
- local target = 100
- -- Generate numbers from 0 to 1
- local nums = {}
- for i = 1, numCount do
- nums[i] = math.random()
- end
- -- Get their total
- local total = 0
- for i, v in ipairs(nums) do
- total = total + v
- end
- -- Replace each number with the floor of target * original / previous total while getting new total
- local total2 = 0
- for i, v in ipairs(nums) do
- nums[i] = math.floor(target * v / total)
- total2 = total2 + nums[i]
- end
- -- Spread the remainder evenly (ish, could involve random here but this is good enough)
- -- This repeating loop could probably be replaced with just a loop that goes over every number once
- local i = 1
- for _ = 1, target - total2 do
- nums[i] = nums[i] + 1
- i = (i - 1 + 1) % numCount + 1
- end
- -- Check final total
- local total3 = 0
- for i, v in ipairs(nums) do
- total3 = total3 + nums[i]
- end
- print(target == total3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement