Is there a way to use math.random() within a limit?

Hi there,

I’m wondering if there is something I can use that would do something similar to math.random() but would not go over a set value (“Capacity Storage System”)

Example:

Player gets a random number eg: (1,5) and adds to a total 1-5/30
Max the number can go to is 30 nothing more.

Thanks,

Well math.random() has two parameters, the minimum value and the maximum value. Just put the maximum value as the second parameter when you call the function

If I’m understanding correctly, I believe you answered your own question.

math.random(1,30)

1 being the minimum and 30 being the maximum. This would return a number between 1 and 30

Yes but 30 would be far to high id need a much lower range using the first two parameters

I don’t quite understand your question.

math.random(1,30)

Again, this would return a number between 1 and 30 which is in line with your parameters

Use math.clamp to clamp a value between certain numbers then. math | Roblox Creator Documentation.

i.e. math.clamp(total + math.random(1, 5), 1, 30)

Try this -

if (math.random(1,5) / 30) ~> 30 then
--// your code
end

A number ranging from 1 - 5 will be selected and then divided by 30 - if the result is greater than 30 the statement will not be satisfied.

what is going on, why is this thread so long

just do

math.random(minNumber,maxNumber)
1 Like

this will never be greater than 30. 1-5 / 30 will never ever be greater than 30, im not sure what the point of this is

exactly, if you want a number to be bigger after dividing then divid it by a fraction or decimal greater than 0 but less than 1

I’m genuinely confused by this thread

what are you confused about so I could help explain?

I’m so confused why the second post was not marked as solution and why this thread is 13 posts deep

Let me give a realistic example of what I’m trying to do here:

You kill an NPC and receive 1,5 Cash and it gets added to a Storage UI with a Capacity of (“30”) it must not go over 30 and a simple math.random(1,30) will not work as that would be far to easy to reach maximum capacity.

You can use either math.clamp or math.min for this.

math.min uses the smallest number for given values.

local number = 0

for i = 1, 20 do
    -- math.min(values ... )
    number = math.min(number + math.random(1, 5), 30)
    print(number)
end

math.clamp meanwhile keeps the number between two values.

local number = 0

for i = 1, 20 do
    -- math.clamp(value, minimum,  maximum)
    number = math.clamp(number + math.random(1, 5), 0, 30)
    print(number)
end

Or, you can let roblox handle it by using the instances: “DoubleConstrainedValue” or “IntConstrainedValue” and setting their respective properties.

1 Like

oh ok I thought you were confused about my post for a second there :sweat_smile:
but yea idk why the thread is still going

Because they want it so math.random(1,5) happens several times and adds to a cumulative total, which is 30. That’s different than just math.random(1,30).

they worded it in a weird way but thanks for saying this

1 Like

okay so what you are trying to do is very simple

local total_number = 0

-- then when ever you want to add a number
total_number += math.random(1,5)
if total_number >= 30 then 
   total_number = 30
end
-- I mean this is very basic but it does the job, if you want you can make it fancier
3 Likes