Hello I was wondering how I would change a percent/decimal chance to a 1 out of (number) for a game like infinite rarities. here is a example

This is the first module

local RarityPicking = {
}
function RarityPicking.chooseIndex (rarityTable, Luck)
local newRarityArray = {}
local totalWieght = 0
for index, rarity in pairs(rarityTable) do
local wieght = rarity[2]
local NewWeight = wieght - Luck
if NewWeight < 1 then
continue
end
local fraction = (1/NewWeight)
totalWieght += fraction
newRarityArray[index] = {fraction}
end
local random = Random.new()
local rnd = random:NextNumber(0, totalWieght)
local selectRarity = "Coal"
local AcummulateWeight = 0
for index, rarity in pairs(newRarityArray) do
AcummulateWeight += rarity[1]
if rnd <= AcummulateWeight then
selectRarity = index
break
end
end
for i,v in pairs(newRarityArray) do
-- This is where I was planning on doing the rarity thing for the gui
end
return selectRarity
end
return RarityPicking

This is the 2nd module that is for the actual rarities

local Rarities = {
["Common"] = {1,1},
["UnCommon"] = {1,3},
["Rare"] = {1,6},
["SuperRare"] = {1,24},
["Cool"] = {1,41},
["Epic"] = {1,64},
["SuperEpic"] = {1,270},
["Legendary"] = {1,648},
["SuperLegendary"] = {1,2000},
["UlraLegendary"] = {1,8000},
["Mythic"] = {1,15000},
}
return Rarities```
Now I am trying to make it so it can figure out this rarity for what it would be with luck
if you need more information just ask please and thankyou for your support!!!

I answered something very similar to this a couple hours ago, take a look because I think it effectively accounts for rarities (weights) in probability and selection:

Nevermind I think I misunderstood. Are you trying to convert a decimal to its fractional representation or apply a ‘luck’ to a set of weights?

yeah I am basically trying to convert it to a fraction using weighted luck but make the 2 numbers in the fraction separated by a comma , instead of a slash /

You can apply a luck effect to any given table of weights with a variety of methods. Here, I simply calculated the average of all weights and made the luck value act as an alpha for how close each weight gets to the ‘normalized’ average:

local selection = {
['godly fish'] = 1;
['golden fish'] = 100;
['talapia rizz'] = 500;
['cod modern warfare'] = 1200;
['stank tuna'] = 3000;
['a rock'] = 5000;
};
local function applyLuck(weights: { [string]: number }, luck: number)
local sum = 0;
local count = 0;
for _, weight in weights do
sum += weight;
count += 1;
end
local avg = sum / count;
local newWeights = {};
for item, weight in weights do
newWeights[item] = weight + (avg - weight) * luck; --simple lerping of each weight to the normalized average
end
return newWeights;
end
applyLuck(selection, .5); --returns a new table of weights that are considered to be affected with 50% luck

Result of the function call:

This satisfies the notion of luck decreasing the odds of more common elements in the table and increasing the odds of less common elements. Keep in mind that you may want to implement a different logic for luck since this method is rather rudimentary.

As for expressing a weight in terms of a fraction with 1 as the numerator, simply determine the odds (that sum to 1) in decimal form by dividing each weight by the sum of all weights, then find the reciprocal of each decimal (1 / decimal) which serves as the denominator of the fraction:

local function asFractions(weights: { [string]: number })
local sum = 0;
for _, weight in weights do
sum += weight;
end
local fractions = {};
for item, weight in weights do
fractions[item] = '1 , ' .. sum / weight; --calculation here is equivalent to 1 / (weight / sum)
--not sure why but you wanted a comma instead of a slash
end
return fractions;
end
asFractions(selection);

Result of the function call:

The fractions here obviously aren’t visually appealing because the weights used don’t seamlessly convert to perfect fractions. Try playing around a little with the weights (such as changing them to fractions that perfectly add to a power of 10). If you don’t understand something, I can probably clarify if you let me know.