How would I start off coding an rng game? would I create a module with the rewards and the odds of them. Then would I make a random num generator such as Random.new():NextNumber(0,100)?
In the test RNG game that I made, I wrote the roll script sort of like this:
module.Rarities = {
-- ["Rarity"] = Integer (higher number = rarer rarity)
}
module.Roll()
local random_number = math.random(1, 10000)
local Rarity = {}
for r, v in module.Rarities do
if random_number >= v then
if not Rarity then
Rarity = {r, v}
elseif v > Rarity[2] and random_number >= v then
Rarity = {r, v}
end
end
end
return Rarity
end
I’m sure it’s not the most effective way of writing a roll script, but at least it works if you don’t have any other ideas on how to do it.
As for picking the number values of each rarity, just experiment with numbers until you are satisfied with the results.
The script picks the highest possible rarity that’s lower than the number generated.
If you have any other questions, I’ll try my best to answer them.
My lotto data module script:
local Data = {
-- Lotto Name, Weight/Ratio
["Basic"] = {
OneIn = 1;
RewardRange = {1,100};
};
["Lucky"] = {
OneIn = 50;
RewardRange = {100,5000};
};
["Fortune"] = {
OneIn = 500;
RewardRange = {5000,250000};
};
["Wealth"] = {
OneIn = 5000;
RewardRange = {250000,10000000};
};
["Galactic"] = {
OneIn = 50000;
RewardRange = {10000000,1000000000};
};
};
return Data
My roll script:
local function ChooseLottoEntry()
local totalWeight = 0
for _, Lotto in pairs(Data) do
totalWeight = totalWeight + Lotto.OneIn
end
local randomNumber = math.random(1, totalWeight)
local accumulatedWeight = 0
for _, Lotto in pairs(Data) do
accumulatedWeight = accumulatedWeight + Lotto.OneIn
if randomNumber <= accumulatedWeight then
return Lotto
end
end
end
The problem is the rarest lotto is chosen too often for some reason
i wish i could help but i’m really bad at math sorry thats the best i can do
using weights for something like this is not viable. you would be better off with picking a random integer between 1 and some current rarity in a table do a check if its <= 1 and if its greater than the current rarity value then that is that picked rarity. implenting luck wouldnt be too hard either. you could just divide the part you pick a random integer with luck, so something like Random.new():NextInteger(1, rarity) / Luck
an idea I have is to use the gaussian distribution, if you search it up on google you can see that the area is all sort of compressed towards the center, and this would be what you want. You can convert the numbers into the rng objects if you want to, for example numbers really close to zero (the more common values) would get converted to common objects, and viceversa. i’m too lazy to implement this into lua rn, but when i’m done, i’ll update this post containing the code that i made.
19:22:02.916 ReplicatedStorage.RNG:19: attempt to compare nil < number - Server - RNG:19
local rng = {}
rng.Rarities = {
-- ["Rarity"] = Integer (higher number = rarer rarity)
["Basic"] = 1,
["Rare"] = 10
}
function rng.Roll()
local random_number = math.random(1, 10000)
local Rarity = {}
for r, v in rng.Rarities do
if random_number >= v then
if not Rarity then
Rarity = {r, v}
elseif v > Rarity[2] and random_number >= v then
Rarity = {r, v}
end
end
end
return Rarity
end
return rng