How would I go about implementing Double Luck for Random "Choose" system?

Hello I’ve been wondering how can I achieve something like Double Luck as you probably know from many games for example Bubble Gum Simulator etc. have a Pet System that is randomly choosing Pet and you can get some kind of “Luck bonus” so you will have better chance to get the Pet… my question is how would I achieve it?

Randomizer:

function ChoosePet(Egg)
	local Data = Eggs[Egg]
	local Pets = Data["Pets"]
	local TotalWeight = 0
	for i,v in pairs(Pets) do
		TotalWeight = TotalWeight + v.Rarity
	end
	local Chance = math.random(1,TotalWeight)
	local Counter = 0
	for i,v in pairs(Pets) do
		Counter = Counter+v.Rarity
		if Counter >= Chance then
			return v.Name
		end
	end
end

Just double the weight of “good ones”.

It won’t exactly double the chance of the good ones, but to do that, you will have to lower the weight of bad ones as well.

Yea I know what you mean but I didnt find it efficient to do.

It’s probably not bad enough to worry about. Computers are powerful enough that you almost never need to favor the most efficient method over the easiest and cleanest. This is what famous Dev-Emperor God-Scripter Sir Donald Knuth referred to when he said “premature optimization is the root of all evil”

This is a method that isn’t too difficult either, but it will be less efficient when you have less than maybe 50 chances when compared to a raffle.

I still recommend a raffle system until it actually becomes a problem.

Alright but what’s difference between your code and mine? It’s basically the same or I am wrong?

Thing is, there is no other way to do it.

The fact that you recalculate the weight every time, uses global variable, hash tables instead of arrays, is already inefficient enough. Enough said.

Yea got it, I thought about same method before even making this post with hope there is another way how to get around this.

Efficiency in mind, I wouldn’t know. The difference is the setup though since someone asked for a script with percentage inputs rather than a raffle. I don’t necessarily recommend it, but it is there if you like it better. I’ll be honest, I misread your initial script as a raffle to begin with

In any case, the way to pull it off is to manipulate the data coming in to the randomizer. That’s why I recommend a raffle system.

local colorTable = { -- Easy to read. You get an item, and you assign it a number for how many tickets it gets in the raffle.
	["Bright blue"] =   {gunTeir_3=2, gunTeir_2=4, gunTeir_1=6, ammoTeir_2=6, ammoTeir_1=8, meleeTeir_2=6, meleeTeir_1=8},
	["Navy blue"] =     {gunTeir_5=2, ammoTeir_5=3, ammoTeir_4=4, ammoTeir_3=4, gunTeir_4=3, gunTeir_3=2, attachment=5}
}

for color, tab in pairs(colorTable) do -- Initialization function, to add the items to the raffle.
	local newTab = {}
	for loot, chances in pairs(tab) do -- Example pairing: loot = gunTeir_3, chances = 2
		for i = 1, chances do -- Insert gunTeir_3 or similar loot several times to give it multiple chances of being picked each time.
			table.insert(newTab, loot)
		end
	end
	colorTable[color] = newTab -- Replace the original clean table with our new raffle table.
end


-- Later in the script.
local tab = colorTable[color] if not tab then return end -- Get the table based on color, and cancel if it doesn't exist.
local loot = tab[math.random(#tab)] -- Pick a random raffle ticket from the table.

Once again this code was borrowed from another post of mine because I’m on mobile. If someone is lucky, just add a couple more tickets to the raffle before drawing a number. It’s quite practical for cleanliness and easiness to modify.

You can pre-make the other table if you can afford the memory overhead, that’s the only way.

Also, pre-calculate the total weight.

Yeah I’ve came across this topic when I was reading the code from the another one that you mention, thank you for advice and for good idea I will see if I am going to implement it to the code in OP.

1 Like

Alright I will try to work with it a lil bit thank you for help :smiley:!