How can I make it that every math.random give you random number that when plussed, the result is 100?
Here’s my script
local random1 = math.random(1,100) -- Result is 18 for example
local random2 = math.random(1,100) -- Result is 22 for example
local random3 = math.random(1,100) -- Result is 57 for example
local random4 = math.random(1,100) -- Result is 13 for example
-- Now, if you print( random1 + random2 + random3 + random4 ), It'll give you 100.
-- How can I implement that system?
Why not using another variable set to 100 and then every math.random reduce this variable by the result. Then, the next math.random would have his max set to the result?
To solve the problem for getting exaclty 100, why not set the fourth variable to max, since 100 - (the first three numbers in the table) = Max (After the for loop).
u have to get difference between random values. in this case each random value will be subtracted by the previous to make sure it doesnt exceeds 100 and does top up with previous one to give the result of 100. this way u can get the fixed sum
function generateRandomNumbers()
local random1 = math.random(1, 100)
local random2 = math.random(1, 100 - random1)
local random3 = math.random(1, 100 - random1 - random2)
local random4 = 100 - random1 - random2 - random3
return random1, random2, random3, random4
end
local random1, random2, random3, random4 = generateRandomNumbers()
print(random1 + random2 + random3 + random4) -- 100
Hey This system does indeed work most of the time. However there is a couple of issues with this, which while, can be rare, can happen causing an error.
The main issue, Is that if random1is 100, your code will start to throw errors…, Like this:
local random1 = 100 -- in this example...
local random2 = math.random(1, 100 - random1) -- Well this becomes math.random(1, 0).. And we can't have that...
This can also Happen if random1 + random2 already equals 100, and therefore generating random3 will throw an error.
Again this is rare, but It’s not too rare, Its atleast >1%, which can be troubling.
A simple fix is just include 0… Thats all that has to be done to fix it, as math.random(0, 0) will return just 0, instead of throwing an error.
function generateRandomNumbers()
local random1 = math.random(0, 100)
local random2 = math.random(0, 100 - random1)
local random3 = math.random(0, 100 - random1 - random2)
local random4 = 100 - random1 - random2 - random3
return random1, random2, random3, random4
end
local function FillWithRandom(Parts: number, Total: number)
local Numbers: {number} = {}
-- {number} is an typed array where it is filled by numbers.
for i = 1, Parts -1 do
local Number = math.random(0, Total -(Parts -i))
Total -= Number
table.insert(Numbers, Number)
end
-- Inserts the remaining to the array
table.insert(Numbers, Total)
return Numbers
end
---
local RandomNumbers = FillWithRandom(2, 100)
local Total = 0
for index, number in ipairs(RandomNumbers ) do
print(`{index}: {number}`)
Total += number
end
print(`Total: {Total}`)
-- Theoric output, given the first index is equal to 40 and the second one is equal to 60:
-- 1: 40
-- 2: 60
-- Total: 100
This function takes two numbers as arguments; Parts amount and Total amount. Returns a array filled by random numbers.
You might aswell use unpack() to put them in declaration format, eg: