Yes! this is an easy way to determine a chance of something happening.

When you set up a variable with math.random() and put a range in there, you can think of it as a percentage of 100%

For example, inserting 1,5 would be a 20% chance because 100/5 = 20

In your case, you have 100/4 which is 25

An easy way for you to make a “random chance” function would be something like this:

local function getChance(range) --Insert a range greater than 1
local index = math.random(1,range) --We determine the chance based off of these two values
if index == 1 then
return true
else
return false
end
end
--[[
An easy way to determine what percentage youd have is to take 100 divided by the range
100 / 4 == 25%
]]--
getChance(4)
print(result)
--[[OR alternatively you could do]]
if getChance(4) then
print("lucky")
else
print("No")
end

Still a 25 percent chance. Because it generates 1 of 4 numbers, it has a 25 percent chance of getting 1,2,3, or 4.

If you did:

if chance == 2 or chance == 3 then
end)

You’d have a 50 percent chance of printing lucky, because both numbers have a 25 percent chance, adding up to 50 percent. 1,2,3 would make 75 percent chance of getting lucky, and 1,2,3,4 100 percent.

Yes, any number within the range of the two numbers should work.

Imagine you have 4 apples and one of them is gold. When you have the if statement and place a number in it, all you are doing is saying which apple is the golden one, while the computer is selecting a random one out of the bunch.

Yes, in fact if you wanted to you could even make an array and use this for that.

local loot2 = { --Here duplicate items have different chances, because they are more common than others
"Lego brick",--30%
"Lego brick",
"Lego brick",
"dog",--10%
"Cat",--10%
"Banana",--20%
"Banana",
"Nyan cat",--10%
"Toaster waffle",--10%
"Watermelon"--10%
}
local function getLoot2()
local index = math.random(1,#loot2) --Instead here we are going to the length of the array, as we need to include all items
return loot2[index] --Sending back the item we randomly selected
end
print(getLoot2)

You can create a dictionary containing the name of an item, along with its percent chance, given that the sum of all percentages totals to 100%.

For example:

local ItemList = {
Boat = 10,
Train = 20,
Car = 30,
Plane = 40
}
function GetItem()
local ItemSelection = {}
for Item, Chance in pairs(ItemList) do
for i = 1, Chance do
table.insert(ItemSelection,Item)
end
end
return ItemSelection[math.random(#ItemSelection)]
end

This code bit would use the specified chance percentages in order to pick an item from the item list. In this example, a Boat has a 10% chance of being chosen, a train has 20% of being chosen, and so on and so forth.

That code works if you are aiming to provide a certain event with a 25% chance of occurrence. However, the code I provided allows you to explicitly set predetermined percentages for any and every item in the list. I tried to write it so it’s easier for you to use, but whichever method you prefer, go for it.

Essentially what my code does is create a large table that contains, for example, 20 percent Trains and 40% planes, with the other items. It makes it so that they actually have that percentage of being chosen.

Ideally, yes. If it goes over 100, than the percentages are not the same percentages. For example, if they total up to 200, they will only have half the percent as if it totaled to 100.