Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting Random Value with MathRandom?

I want to take a random value like:

Common: 60%
Uncommon: 25%
Epic: 10%
Legendary: 5%

Then Print a Random Value

Like Case Opening System

Can Someone Help Me to Do It?

4 comments

0

Do you mean you want it to be increasing by 5 or any number between 1 to 100?

0

@Lightning_Game27 Like murder mystery opening case, egg on adopt me. Stuff like that.

0

@TheSaver31 could you please clarify EXACTLY what you need?

0

@Lighting_Game27 LIKE adopt me egg system, like when i buy random sword. It will give me a random sword by a chance

3 answers

✓ Accepted answer

Instead of "math.random" i recommend you using Random because it supports decimal numbers instead of "math.random" which supports whole integers only.

The best way of generating random rarity is by using weight system, it's like you add chances of all rarities into one number, then generate random number in-between them, loop through every rarity chance and check if the chance is lower or is the random number:

-- 1
local Rarities = {
	{"Legendary", 5},
	{"Epic", 10},
	{"Uncommon", 25},
	{"Common", 60},
}

local function GetRandomRarity()
	-- 2
	local TotalValue = 0
	
	for _, Rarity in ipairs(Rarities) do
		TotalValue += Rarity[2]
	end
	
	-- 3
	local RandomNumberGenerator = Random.new()
	local Chance = RandomNumberGenerator:NextNumber(0, TotalValue)
	
	local Value = 0
	
	-- 4
	for _, Rarity in ipairs(Rarities) do
		Value += Rarity[2]
		
		if Chance <= Value then
			return Rarity[1]
		end
	end
	
	-- 5
	return nil
end

Here is explanation of each step.

  1. This is list of rarities, it is made up of tables and not keys like Legendary = 5 to allow us to use "ipairs" to keep the order from lowest to highest, with keys you would have to use "next"/"pairs" loop which would not care about the order, bad. The first value in each table is name of the rarity and second one is the chance. Rarities must be sorted from rarest to most common to work.
  2. This is where you combine all values of each rarity into total rarity from which you generate the random number. You can replace it with math.random(1, TotalValue) or RandomNumberGenerator:NextInteger(1, TotalValue) to unsupport decimal numbers.
  3. Uses "Random" to create new number generator with new seed and generates random number from 0 (to support decimal numbers) to "TotalValue" which is the total value of all rarity chances from step 2.
  4. This will loop through each rarity and will increase "Value" by it's drop chance, then it will check if that random generated number is lower or is the value, if it is it will return name of the rarity as string.
  5. If in some impossible case it won't generate anything, if for example rarity table is empty, it will return nil instead of void, you can change the return value for "Empty" or "None" too if you need, does not matter.

2 comments

0

i dont understand any of that but why not accept it

0

oh sorry i thought you know about loops, you should stick to wizard's answer then, his is simpler.

I'm not sure if you're overcomplicating your own thoughts, but this is very straightforward.

The four chances should add up to be 100, considering that the maximum percentage value is 1, and the minimum is 0. Percents work as x/100, with x being the number represented as the percent, like 60%, which is equivalent to 60/100, which is 0.6. That being said, your math.random() should actually look like this:

local value = math.random(1, 100) -- Start at 1 since it is the lowest non-zero chance possible. The range 1-100 is also 100 itself; 0-100 has a range of 101.

Now you should define your ranges. Going off your given percentages, your ranges are going to look like this:

  • Any number from 1-60 yields a common item.
  • Any number from 61-85 yields an uncommon item.
  • Any number from 86-95 yields an epic item.
  • Any number from 96-100 yields a legendary item.

There are two possible ways to set these ranges. The simplest way to do it is to check them directly:

if value >= 1 and value <= 60 then
	-- Common
elseif value >= 61 and value <= 85 then
	-- Uncommon
elseif value >= 86 and value <= 95 then
	-- Epic
elseif value >= 96 then
	-- Legendary
end

The other way, albeit more of a hassle, is to define NumberRanges. The difference here is, after creating NumberRanges using the NumberRange.new() constructor, you will replace the numbers in the above if statement(s) with NumberRange.Min and NumberRange.Max, respectively.

That is all you need to know about what you have asked.

OK, so I haven't been programming in Roblox Lua for a while now, so please do not expect it to work right away!

I would suggest giving 4 different variables different math.Random values and then printing it all together!

local commonValue -- Define values
local uncommonValue
local epicValue
local legendaryValue

game.Players.PlayerAdded:Connect(function() -- When a player is added, fire function
	commonValue = math.random(1, 100) -- Set value to random integer between 1 to 100
	uncommonValue = math.random(1, 75)
	epicValue = math.random(1, 50)
	legendaryValue = math.random(1, 25)

	--If You want it to round up or down
	commonValue = math.ceiling(commonValue) -- Round value up to nearest 10
	uncommonValue = math.ceiling(uncommonValue)
	epicValue = math.ceiling(epicValue)
	legendaryValue = math.ceiling(legendaryValue)

	--note: math.ceiling means round up and math.floor means round down

	print(commonValue, uncommonValue, epicValue, legendaryValue)
end)

Hope it work and I hope it helps you out!

9 comments

0

So its like 1, 100. The 100 is the chance?

0

100 is the most common and 1 is rare. 100 is max chance. You can change that to a lower number if you want!

0

WAIT, so it will print all common, uncommon, epic, and legendary value?

0

Yes

0

thats not what i need

0

Please explain more clearly then

0

@Lighting_Game27 LIKE adopt me egg system, like when i buy random sword. It will give me a random sword by a chance

0

You've said that three times now, if people still say "explain more clearly" then you have to understand it doesn't help to just repeat yourself. Explain. More. Clearly.

0

Ok, I will tell you right now that this will not work correctly. The random values should ALL be generated within the same function, not into 4 separate functions. This ensures weight, but dividing it evenly across 4 functions does not factor in weight.

This discussion is preserved as it was. New answers, comments, and votes are closed.