Rng Random System

Hello there, I know you have probably seen all of the rng games popping up all over roblox and you might be asking yourself how the interworkings are made. Well here is some basic code that you can use to randomly get a item from a database based off of rarity!

local items = {
	{
		["Name"] = "Common",
		["Id"] = "common",
		["Rarity"] = 1
	},
	
	{
		["Name"] = "Uncommon",
		["Id"] = "uncommon",
		["Rarity"] = 5
	},
	
	{
		["Name"] = "Rare",
		["Id"] = "rare",
		["Rarity"] = 30
	},
	
	{
		["Name"] = "Epic",
		["Id"] = "epic",
		["Rarity"] = 80
	},
	
	{
		["Name"] = "Legendary",
		["Id"] = "epic",
		["Rarity"] = 300
	},
	
	{
		["Name"] = "God",
		["Id"] = "god",
		["Rarity"] = 100000
	}
}

local function getRandomItem()
	local possibleItems = {}
	for _, item in next, items do
		local number = math.random(1, item.Rarity)
		if number ~= 1 then continue end
		
		table.insert(possibleItems, item)
	end
	
	local bestItem = nil
	for _, possibleItem in next, possibleItems do
		if bestItem == nil then bestItem = possibleItem continue end
		if possibleItem.Rarity < bestItem.Rarity then continue end
		
		if possibleItem.Rarity == bestItem.Rarity then
			local switch = math.random(1, 2) == 1
			if not switch then continue end
		end
		
		bestItem = possibleItem
	end
	
	return bestItem
end

-- DELETE THIS
while true do
	local item = getRandomItem()
	print(item.Name, item.Id, string.format("1 / %d", item.Rarity))
	
	task.wait()
end

How dose it work:

Well it loops through all of the items in the items table and generates a random number from 1 to rarity. Then if the number is 1 it adds it to a list of possible items. Then It gets the best item from the list and returns it. Also if 2 items are the same rarity it just picks a random one.

How to use this code:

All you need to do is add your own custom items to the list.
You should probably put them inside a module script and then require that.

And then when you want to get a random item just call the getRandomItem() function!

Thanks for reading and if you want to see this system in action I made a video using something similar to this here.

2 Likes

why not use the already created weighted luck system?

weighted luck system seems like less lines, easier to understand, and it is very well explained (mathematically)

1 Like

You know it is easier and take less line of code to simply do the table like that:

local items = {
    ["Name"] = {Id, Rarity};
    ["Name"] = {Id, Rarity}
}

Also, this method is not bad, i remade it in a better way if you want:

local PrizeTable = {
	["Rarity1"] = 1; --1/1
	["Rarity2"] = 1; --1/1
	["Rarity3"] = 10; --1/10
	["Rarity4"] = 100; --1/100
	["Rarity5"] = 1000; --1/1,000
	["Rarity6"] = 10000; --1/10,000
	["Rarity7"] = 100000; -- 1/100,000
	["Rarity8"] = 1000000; -- 1/1,000,000
	["Rarity9"] = 10000000; -- 1/10,000,000
	["Rarity10"] = 100000000; -- 1/100,000,000
	["Rarity11"] = 1000000000 -- 1/1,000,000,000
}

local function Randomize()
	local SelectedPrizes = {}
	local BestPrize = nil
	
	for Prize, Chance in pairs(PrizeTable) do
		if math.random(1, Chance) == 1 then
			table.insert(SelectedPrizes, Prize)
		end
	end

	for _, Prize in pairs(SelectedPrizes) do
		if BestPrize == nil then
			BestPrize = Prize
		elseif BestPrize and PrizeTable[Prize] > PrizeTable[BestPrize] then
			BestPrize = Prize
		elseif BestPrize and PrizeTable[Prize] == PrizeTable[BestPrize] and math.random(1, 2) == 1 then
			BestPrize = Prize
		end
	end
	
	return BestPrize
end

-- Test on 100,000 rolls - check the table result in output
local TotalPrizes = {}
for Count = 1, 100000 do
	local BestPrize = Randomize()
	
	if not (TotalPrizes[BestPrize]) then
		TotalPrizes[BestPrize] = 1
	else 
		TotalPrizes[BestPrize] += 1
	end
end
print(TotalPrizes)
2 Likes

This will help me alot, thanks! I have seen some other developers mention you could shorten the code by putting them in single line tables, which i may do. Thanks!

1 Like

Just made my own and thought I would share it here. Same could be said for pretty much anything anyone has posted as there are alternate versions.

1 Like

Thank you sirr‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎