How can I make an algorithm that randomly chooses objects based on its rarity (or a fraction)

Hello, I am currently making a script that randomly chooses an item based on its rarity to give to the player. I am very bad with math so I don’t know how I would make a system where item’s are chosen ranomly based on a fraction. For example, one items chance of getting selected is 1/1000000 whilst another is 1/25. If anyone can help me out I’d really appreciate it. Thanks for reading : )

You could have a module system like this:

module.ItemRarities = {
	["Legenary"] = 5, -- 5% Chance
	["Epic"] = 20, -- 20% Chance
	["Rare"] = 35, -- 35% Chance
	["Common"] = 40 -- 40% Chance
	-- Note: All numbers should add up to 100
}

Then you would create another table, this will be where the items are saved:

module.Items = {
	["Legenary"] = {
		-- Item Location
	},
	["Epic"] = {
		-- Item Location
	},
	["Rare"] = {
		-- Item Location
	},
	["Common"] = {
		-- Item Location
	},
	-- Your items mostly would be in ReplicatedStoage in folder called Weapons; So You would ReplicatedStorage.Weapons["WeaponsName]
	
}

Now, all you would have to do is have a system that chooses a random number, and if the number <= the chance then it will get a random item from the table above ^; You could add this into a function so when you call the function it will generate a new random item.

local RandomNumber = math.random(1, 100)
local Number = 0

for Rarity, Chance in pairs(module.ItemRarities) do -- For Loop through the ItemRarities Table
	Number = Number + Chance
	if RandomNumber <= Number then

		local ItemTable = module.Items[Rarity] -- Gets the Rarity Key
		local ChosenItem = ItemTable[math.random(#ItemTable)] -- Chooses a random weapon inside of the Key
		
		return ChosenItem
	end
end

This the main idea. This script does not give the item to player, it simply just chooses a random Item. If you want it to get added to your inventory, you would have to do that with your current inventory system if you have one.

4 Likes

Okay tysm I’ll try this out rn : D

1 Like

Weight implementation:

local chance = {
	common = 11;
	rare = 22;
	epic = 33;
}

do
	local sum  = 0
	for _, weight in next, chance do
		sum += weight
	end

	local total = 0
	--// change them into decimal part.
	for rarity, weight in next, chance do
		
		local pct = weight/sum
		
		chance[rarity] = pct + total
		total += pct
	end
end

local thisrandomvalue = math.random()

for rarity, x in next, chance do
	if thisrandomvalue <= x then
		print(rarity) break
	end
end
1 Like

try this vid:

1 Like

Hey so I tried out what you posted, and when trying to print the item that was chosen, the script prints nil. I followed everything you told me to do, but where you have local ChosenItem = ItemTable[math.random(#ItemTable)] I changed to local ChosenItem = ItemTable[math.random(1, #ItemTable)] to try to fix it from not erroring nil. I have this being printed by another serverscript requiring the module and firing the function.

Hello, you still need a solution?

1 Like

nvm was using ipairs instead of pairs lol, tysm for this

1 Like

where does the module variable come into play since it comes up with a red line for me.