What is best way to create RNG?

What is easiest way to create RNG system with table of chances?

use math.random and connect the output of that to something

1 Like

I need to store values in table having thier own chance.

in that case, you can have a table of values with their own number. that number will correspond to the max number math.random will take. so for example…

local table_of_rarities = {
     ["Some Item Name + Rarity"] = 125,
     ["Another Item Name + Rarity"] = 10,
     ...
}

math.random(1, table_of_rarities[1]) -- this is now (1, 125)
math.random(1, table_of_rarities[2]) -- this is now (1, 10)

just an idea

You could do something like this:

local itemDataBase = {
	Copper = { SellPrice = 8, Rarity = 75, etc.. },
	Silver = { SellPrice = 23, Rarity = 45, etc.. },
	Gold = { SellPrice = 46, Rarity = 25, etc.. }, 
	Diamond = { SellPrice = 72, Rarity = 10, etc.. },
}

local R = Random.new()
local function getOreChance(ore)
	return R:NextInteger(1,itemDataBase[ore].Rarity)
end
local function getRandomOre()
	local t = {"Copper","Silver","Gold","Diamond")
	for i = 1, #t do
		if getOreChance(t[i]) == 1 then
			return t[i]
		end
	end
	return "Stone"
end

print(getRandomOre()) --> Gold
print(getRandomOre()) --> Copper
print(getRandomOre()) --> Diamond
5 Likes