RandomChances - Easy to use (ToS compliant!) random chances library

This is a really simple library I made that handles “random chance” events/item selections for you. Its easy to use, ToS complaint, and is unbiased (has inbuilt “security” to prevent seed guessing attacks/etc).

Example:

local Rand = require(script.RandomChances)

local Res = Rand.Run(
{
	--Each item has to add up to 100% in total. The amount you put in is the percentage you actually get such item.
	["epic item"] = 20,
	["even epicer item"] = 20,
	["even epicer-er item"] = 20,
	["super epic item"] = 30,
	["omega epic item"] = 5,
	["MOST epic item"] = 5,
})

print(Res) -- "epic item" (for example, will change every time you call it)

Some statistics of using this:

local Count = {}
local Events = 
{
	["epic item"] = 20,
	["even epicer item"] = 20,
	["even epicer-er item"] = 20,
	["super epic item"] = 30,
	["omega epic item"] = 5,
	["MOST epic item"] = 5,
}

for I, V in pairs(Events) do
	Count[I] = 0
end

for I = 0, 100000 do
	local Res = Rand.Run(Events)
	
	Count[Res] = Count[Res] + 1
end

for I, V in pairs(Count) do
	print(I, V)
end

--[[
Output:

epic item 19835
even epicer item 20659
even epicer-er item 19851
super epic item 29590
omega epic item 4992
MOST epic item 5074
]]

The library itself uses the Random API - the best option we currently have for random numbers. While it isn’t very secure, its all we got so I attempted to use it in the best way possible.

Each chance is set into a translation table that specifies the percentage bounds of each item/event. For example, 5 random items of 20% each would be split into the following entries:

Entry 1: 0-20
Entry 2: 20-40
Entry 3: 40-60
Entry 4: 60-80
Entry 5: 80-100

After that, it generates a random number from 1-100 and then searches through the translation table to see which entry it corresponds to.

The RNG is re-seeded every 1-75 iterations with the current time (the seed that Roblox itself uses) and the last random number generated to hopefully provide some more entropy.

Link to module. Enjoy!

10 Likes

This topic was automatically closed after 1 minute. New replies are no longer allowed.

Hmmm, I really want to test this. Can’t we just use rarity weights and calculate the percentage based on rarityWeight / totalWeight?

2 Likes