What this script does
Test a weighted result table and inspect the exact normalized chance produced by each weight. No Steal An Egg odds are included.
- Best for
- Prototype loot tables and teach weighted probability inside a Studio project.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau runtime tested
Official Luau 0.735 parsing and strict analysis pass, and the module is covered by the pure-module runtime behavior harness.
Included behavior
- Accepts any positive weighted entries
- Returns both the result name and normalized chance
- Can list every calculated chance
- Supports an injected number generator for repeatable tests
Install in the stated runtime
- 1Create a ModuleScript named WeightedRarity.
- 2Paste the source and require it in your own Studio test Script.
- 3Provide your own fictional or project-owned weights; do not present demo values as live-game facts.
Minimal usage example
local WeightedRarity = require(script.Parent.WeightedRarity)
local demoWeights = {
{ name = "Common", weight = 70 },
{ name = "Rare", weight = 25 },
{ name = "Mythic", weight = 5 },
}
local seeded = Random.new(1234)
local result = WeightedRarity.roll(demoWeights, function(minimum, maximum)
return seeded:NextNumber(minimum, maximum)
end)
print(result.name, result.chance)Example inputs and result
- Supply
- A list of names and positive weights; the example also supplies a seeded random function.
- Expect
- roll returns the chosen name and its normalized chance for those supplied weights.
These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.
Complete source
No hidden import, minified block, or shortened loader.
--!strict
-- MIT License. For Roblox Studio experiences you own or can edit.
-- Demo weights are fictional. Replace them with values from your own experience.
export type WeightedEntry = {
name: string,
weight: number,
}
export type RollResult = {
name: string,
chance: number,
}
local WeightedRarity = {}
export type NextNumber = (minimum: number, maximum: number) -> number
local function totalWeight(entries: { WeightedEntry }): number
local total = 0
for _, entry in entries do
assert(entry.name ~= "", "Entry names cannot be empty")
assert(entry.weight > 0, "Every weight must be greater than zero")
total += entry.weight
end
assert(total > 0, "At least one weighted entry is required")
return total
end
function WeightedRarity.roll(entries: { WeightedEntry }, nextNumber: NextNumber?): RollResult
local total = totalWeight(entries)
local target = if nextNumber then nextNumber(0, total) else math.random() * total
local cursor = 0
for _, entry in entries do
cursor += entry.weight
if target <= cursor then
return { name = entry.name, chance = entry.weight / total }
end
end
local fallback = entries[#entries]
return { name = fallback.name, chance = fallback.weight / total }
end
function WeightedRarity.chances(entries: { WeightedEntry }): { RollResult }
local total = totalWeight(entries)
local results = {}
for _, entry in entries do
table.insert(results, { name = entry.name, chance = entry.weight / total })
end
return results
end
return table.freeze(WeightedRarity)
4d7410573a1cc90b584b0f6d644f5deb54e033328b4b64855f87f8a0c2f9f2a2Outside this script’s scope
- Reveal private or current Steal An Egg odds
- Change live hatch results
- Contact Roblox or any third-party service
Found a problem or a better approach?
Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.