How would I make a proper RNG system?

So for a project I’m making for fun and to upgrade my skills a little more, I’m doing something inspired by Sols’ RNG.

I’ve tried a weighted system, but I’d prefer higher rarity → more rare and lower rarity → less rare.

Also, no matter what I try the system always prefers David out of the following (this is psuedocode so don’t worry):

David = 1000,
Jill = 1500,
Tom = 40

Help would be greatly appreciated, thanks :slight_smile:

3 Likes

This is how I implemented luck in an RNG type game. (This also takes into account a luck value so you can easily implement potions/upgrades into your game)

local random = Random.new()
local ChancesModule = require(SharedModules.ChancesModule)

local function selectRandom(plr)
	local lastIndex = 1

	for index,value in ChancesModule do
		if (random:NextInteger(1,value[2]) / plr.playerValues.Luck.Value) <= 1 and value[2] > ChancesModule[lastIndex][2] then
			lastIndex = index
		end
	end

	return lastIndex
end

ChancesModule:

local Chances = {
	{"Common", 2, Color3.fromRGB(202, 202, 202), "bad rarity"},
	{"Uncommon", 4, Color3.fromRGB(132, 202, 116), "kinda bad"},
	{"Rare", 10, Color3.fromRGB(61, 64, 255), "wow so rare"},
	{"Epic", 25, Color3.fromRGB(141, 0, 202), "so epic"},
	{"Legendary", 50, Color3.fromRGB(255, 213, 0), "legendary????"},
	{"Mythical", 100, Color3.fromRGB(110,255,224),"no way its a mythical"},
}

return Chances

If you want any further explanation as to how this works feel free to ask.

3 Likes

Thanks, this works like I needed it to!

1 Like

Aspiring scripter here, could you explain how this works to me?

1 Like

Firstly, the script creates a random instance. The reason I chose to use this over math.random is because it has a signficantly higher limit than the math.random’s limit of 2,147,483,647. Second, I require the ChancesModule (a module script) which has all the different rarities and their info stored (name, chance, color, description). Next, the selectRandom function starts at index 1, or the start of the ChancesModule. After the lastIndex has been set, we loop through the whole ChancesModule. While doing this we do random:NextInteger(1, value[2]) since value[2] is the rarity of the item (in this case it is common so the it is picking a random number between 1-2. Then I divide it by the player’s luck, which let’s assume is 1 (default luck). Now we check if the random number between 1 and 2 was <= 1, if it was that means we successfully got the 1/2 chance common, if it wasn’t that means we didn’t get it. This loop goes through every single rarity and does the exact same thing, but the higher the item rarity the harder it is to get it, for example mythical picks a number between 1 and 100, so it is significantly rarer. In The end we return the lastIndex (this is equal to the index of the rarest item that we rolled). If we rolled nothing then it automatically returns index 1 (common).

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.