Is this a bad chance system?

doing local Percentage = math.random(1,100) doesnt have a chance system it just picks random math.ceil(math.random()* 100) makes it hard to get stuff with a percentage of 10

Aren’t you trying to get a number between 1 to 100?

1 Like

as my percentage. math.random(1,100) it could pick 1 10 times in a row math.ceil is an efficient way for percentage. math.random(1,100) is completly random and not based off of the chances

That doesn’t make sense. If you do math.random(1,2), that will be a 50% chance. If you use math.ceil, that will be still random, and it’s worth to note math.ceil has nothing to do with chances.

1 Like

i understand but doing math.ceil(math.random() * 100) its rare to get a number like 8

Isn’t it the same with math.random(1,100)? I still didn’t really get why you use math.ceil

There is an equal chance for every number. If you’re doing math.random(1,100), there will be a 1% chance to get 1, 58, 67, etc.

1 Like

I saw a friend with a working chance system send me his code

As @zamd157 said, there’s an equal chance.
Not sure why your friend did that.

This should work just fine as well, your code just seems a bit longer.

This article could maybe be useful:

It is mostly about creating the “spin effect” but also talks about a weighted value sytem (example: less likely to get rare in a draw than to get common).

I can write my own chance system, I just thought I found a shortcut. But now that I realize its the same as math.random() :v:

1 Like

Back to the question, what you wanna do is take the number from math.random and use see if it is between the span of the numbers you need to get that rarity using inequalities. Here’s an example:

local number = math.random(1,100)

-- lets say common is from 55 - 100, rare 25 - 55, epic 5 - 24 and legendary 1 - 4

if number < 5 then -- if it is smaller than 5 (legendary)

	print("legendary")

elseif number >= 5 and number < 25 then -- if greater or equal to 5 and smaller than 25 (epic)

	print("epic")

elseif number >= 25 and number < 55 then -- if it is greater or equal to 25 and smaller than 55 (rare)

	print("rare")

elseif number >= 55 and number < 101 then -- if it is greater or equal to 55 and smaller than 101 (common)

	print("common")

end
1 Like

It doesn’t matter if you generate a number using math.random(1,100) or math.ceil(math.random()*100)
Both will give the same results with the same expected distribution.

I personally like to skip the 0-100 integer thing and just use 0-1 scale as the percent.

if math.random() < .1 then
end

Again, all methods talked about function identically and work equally well.

2 Likes

Also, math.random() is actually a pseudo-random number generator. Meaning that it tries to get random numbers, but the generated numbers aren’t actually completely random. When called multiple times it can return the same “random” numbers.
Wikipedia article:

This deserves the solution. You gave me a full tutorial on how it works

Not really, the question is Is this a bad chance system? and @LightningLion58 gave you their yes/no. I just gave a system for you to base off.

I think that this could be a dangerous tangent for this thread, but if there is concern around this, devs can use

local rng = Random.new()
if rng:NextNumber() < .1 then
end

or

if rng:NextInteger(1,100) <= 10 then
end

with similar results with a less quirky random number generation.

1 Like

You have a (near) equal chance of getting every rarity with the code you provided. The “rarities” don’t do much. In my opinion that would be a bad luck system, as it doesn’t really take into account the different rarities.

This code currently gives the highest chance to “epic” items with 35%, and only 30% chance to each “rare” and “common”
I believe that that is unexpected.

Oops, I was always weak at maths. I fixed it now.

1 Like