How to make a percent chance for something to happen

I am not sure if this would work, but I use this script for 25% (1/4 as a fraction) chance

local chance = math.random(1,4) -- the numbers come from the fraction above

if chance == 1 then

print("Lucky!")

else

print("no")

end

if this isnt a script for percentage chances then please comment the script

12 Likes

so this would be a 25% of printing lucky?

edit: or is there another script

Indeed. If you translate 1/4 to 100’s it’d be 25/100, making a 0.25 chance. This works perfectly well.

8 Likes

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
4 Likes

what would happen if i did

local chance = math.random(1,4) 

if chance == 2 then

print("Lucky!")

else

print("no")

end

edit: would chance == 2 still work

2 Likes

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.

so this is a random loot table

What do you mean by this is a random loot table?

different chances for different items

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)
1 Like

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.

10 Likes

but would my code work too?

local chance = math.random(1,4) 

if chance == 2 then

print("Lucky!")

else

print("no")

end

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.

2 Likes

Can you explain for this line of code why aren’t you added 1 until its chance

for i = 1, Chance do
			table.insert(ItemSelection,Item)
		end
1 Like

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.

1 Like

Quick question, as you wrote the percentages in the ItemList. Do they need to all add up to 100?

1 Like

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.

1 Like

then can you explain me how dev like do 0.00001% thing ???
please

Check out some of the replies to this thread.

2 Likes