How to generate a random number with a higher chance to have a low value?

Basically, I want to generate a random number between 1 and 1000, being there a higher chance to generate low value numbers, like 25, 64, 120, etc. The only way I found how to do it has a LOT of lines, and is extremely slow, it’s not a good option. How would I be able to do this?

1 Like

I would have something to divide it by.

local number = math.random(1,1000)
local dividend = math.random(10,20)/10
number = math.ceil(number/dividend)
print(number)

Basically, it would be even rarer than it already was to get 1000

1 Like
local lowerGenerator = math.random(1,4)

if lowerGenerator  == 1 then

lowerGenerator = math.random(1,100)

elseif lowerGenerator == 2 then

lowerGenerator = math.random(1,100)

elseif lowerGenerator == 3 then

lowerGenerator = math.random(1,100)

elseif lowerGenerator == 4 then

lowerGenerator = math.random(100,1000)

end

print(lowerGenerator)

This would be one basic way just with math.random.

2 Likes

Works fine! But, still generates quite high value numbers, but thanks! I’ll use it for now.

Oh my… That’s the exact code I was using before, nope, I’m good.

Why was that bad?


I think it looks quite… obvious, doesn’t it? Plus, it still generates very high values if it gets 4. And, as I said, I want something like a chance system to generate it.

I dont thing so and if it generated very high values u could add more if statements but i said its a way just with math.random.

From what it looks like, I think you’re a begginer… Too many if/elseif statements can slow the game’s perfomance, and can also be very slow.

Im not a beginner, im scripting for 2 years, u could be a beginner i said to u in the Text “Its a basic way with just math.random”, do u know what i mean with “A basic way “just” with math.random”?

Alright, I know you said only using math.random, but that’s not my solution, since it is not very fast, but thanks at least.

I could make it a bit lower by adding another number. Like this:

local number = math.ceil(math.random(1, 50*math.random(1,20))/(math.random(10,20)/10))

kind of a long line of code, but it should work

1 Like

Okay no problem


Wow… works perfectly! Just like I was expecting, thank you so much!

Yea, I had to think about this one for a second. Just hope you aren’t using this to rig chances of getting something :joy:

1 Like

Infinite ways to do this.

Perhaps the easiest way is to take the lowest of 2+ rolls.

Local x = 2 --make larger for rarer high numbers.
Local random = random.new()
Local roll = 1000

for i = 1, x do
roll = math.min(roll, random:NextInteger(1,1000))
end

Already out the gate, the odds of getting 1000 are 1 in a million.

5 Likes

Works perfectly too! Thanks as well!

1 Like