RNG luck code question

So I got this code from AI and I am unsure how to add a luck multiplier

local function GenerateRNG()
	
	local totalWeight = 0
	
	for Rarity, Data in pairs(Database) do
		
		totalWeight = totalWeight + Data.Chance
		
	end

	local randomNumber = Random.new():NextNumber(0, totalWeight)
	
	local weightSum = 0

	for Rarity, Data in pairs(Database) do
		
		weightSum = weightSum + Data.Chance
		
		if randomNumber <= weightSum then
			
			local RarityVal = math.round(Random.new():NextNumber(Database[Rarity].ValRange[1],Database[Rarity].ValRange[2]) * 100) / 100
			
			return Rarity, RarityVal
			
		end
		
	end
	
end

I thought maybe I could multiply the chance value by the multi value on this line but it didn’t seem to work instead it made me get commons more often instead.

totalWeight = totalWeight + Data.Chance

Database:

local Data = {
	["Common"] = {
		Chance = 50; -- 1 in 2
		ValRange = {1,99.99};
	};
	["Uncommon"] = {
		Chance = 20; -- 1 in 5
		ValRange = {100,299.99};
	};
	["Rare"] = {
		Chance = 10; -- 1 in 10
		ValRange = {300,499.99};
	};
	["Epic"] = {
		Chance = 5; -- 1 in 20
		ValRange = {500,749.99};
	};
	["Legendary"] = {
		Chance = 0.1; -- 1 in 1000
		ValRange = {750,1000};
	};
	["Mythic"] = {
		Chance = 0.01;-- 1 in 10000
		ValRange = {1000.01,1500};
	};
};

return Data
3 Likes

AI Coding?? shakes head in disappointment

2 Likes

No, this is the only script that I used from AI the rest I did myself like the database I coded

Add up all the chances, generate a random number from 0 to that total, then pick one based on where it lands on a number line made up of segments whose lengths are the chances:
image

1 Like

No i want to know how to make a luck multiplier

1 Like

Luck usually works by throwing out the lowest chances, so you shrink the length of the low rarity lines by some function.

Show me an example

extra text…123

1 Like
--Make it luckier
function enlucken(chances)
	for i, v in pairs(chances) do
		chances[i].Chance = math.sqrt(v.Chance)
	end
end
1 Like

how abt if i’d like to make a custom luck boost such as 2x or 5x

1 Like

You’d have to decide what 2x luck means

1 Like

2 times as lucky

extra text…123

1 Like

what is 1 times as lucky then 123123

1 Like

for ex: if legendary is 5 percent then it should be 10 percent when its 2x luck

the problem is when I multiply the chance luck it will also do so to the common rarity

1 Like

multiply everything except common

1 Like

think of it more like a scale, where you weigh a type, if you increase one side, you’ll have to decide which side to decrease so that your LUCK system is fair and uniform. . . those changes are arbitrary and we can’t help much with it.

just an honest suggestion, stay true to maths, if you want to do x2, then keep this number in mind instead of doing like some RNG where x2 isn’t actually x2

1 Like

What I tried rn is creating a variable for each rarity called luckboostable and if its true then I will multiply the chance for the rarity if its a high rarity

1 Like

this code applies a flat luck multiplier to the total weight, rarer items will benefit more proportionally than the common ones.

-- Function to get a random item based on weighted chances and luck boost
function GetRandomItem(items, luckMultiplier)
  -- Local variables
  local totalWeight = 0
  local randomValue

  -- Calculate total weight of all items
  for _, item in pairs(items) do
    totalWeight = totalWeight + item.weight
  end

  -- Apply luck multiplier (optional)
  if luckMultiplier then
    totalWeight = totalWeight * luckMultiplier
  end

  -- Generate random value
  randomValue = math.random(totalWeight)

  -- Loop through items and check weight ranges
  for _, item in pairs(items) do
    local lowerBound = totalWeight - item.weight
    totalWeight = lowerBound

    if randomValue > lowerBound then
      return item
    end
  end

  -- If no item found (shouldn't happen), return nil
  return nil
end

-- Example usage
local items = {
  { name = "CommonItem", weight = 10 },
  { name = "UncommonItem", weight = 5 },
  { name = "RareItem", weight = 1 },
}

local luckMultiplier = 1.5 -- Increase chance for rarer items

local randomItem = GetRandomItem(items, luckMultiplier)

if randomItem then
  print("You got:", randomItem.name)
else
  print("Error: No item found!")
end

does this clarify things ?

4 Likes

what is weight ?, it is the % of a number?

1 Like

it’s that, picture it as the amount of it you want on the total of all of the weigh…

like if your total weigh is 100 and your common weigh is 40 then ye it means 40%, but only if the total weigh isn’t limited to a specific number, so just try to tweak with it