Why does improperly ordered math.random() input not emit errors?

Just wondering if this has been questioned before

Although my output above is from Luau Playground, the same code doesn’t emit that there exists is an error in Studio’s code editor, and only displays a sign of failure in output

Additionally, behavior that this creates an empty interval is not documented.. I believe?

I assume in the random() function they calculate interval between minimum and maximum value that you provide, which is expected when trying to generate value between 2 numbers. Since “Min” is greater than “Max” interval it calculates is empty, therefore function has no data to choose from.

Yet the error message is a bit confusing at first glance, Roblox should really make a check min<max and make better error message.

1 Like

math.random takes two numbers: x and y
math.random(x,y) and then create an interval beginning from x to y, and picks a random numver from that interval
example: x=2, y=5, the interval created is: [2,3,4,5]
if x is bigger, then interval created is empty: hence the error

and no, during coding, it will not warn about the error as the same way that other similar functions dont, like number.sequence, because type checking does not check comparison between two values, only checks the type, both 2 and 5 are type number, and it just checks the type, which is that two values must be number
the reason for this is quite complicated and would require going deep into why? but most likely is that like most coding languages, most errors are caught after running the program fully, because most of the data is extracted from other libraries/code which has yet to run before, like the math library, it is not running in the background and will only run once you execute the code and so produce an error if the inputs are wrong

1 Like

In Roblox studio it emits same error as you got in luau playground.

Though it doesn’t show “invalid argument” in the editor when arguments are literals

Or, like, is this some sort of programming language typing consistency that I must understand?

If we’re looking at the arguments purely from a type-correctness perspective, they’re valid because they’re both numbers. Type checking only cares about whether the values are the correct type, not their relationship.

Data relationships like min < max are evaluated at runtime and must be handled manually. I doubt there’s a way for Studio to check such relationships—even with literals—before the script runs. But Studio can check types (number or not) at edit time.

Here’s documentation about type checking in Roblox Studio.

That’s just antithetical, right? What’s stopping the type checking system from being able to evaluate comparisons of number literals? I am not that knowledgeable about creating programming languages, so I’d like to know why this is not a capability of Luau. Is it not practical?

Consider an example with the following boolean expression:

I feel like an indicator that something is never run should be displayed.

I am not sure if selene is capable of emitting such information

Also, would being able to evaluate conditions (not with that only containing literals) to check if an if-block would never run a case of boolean satisfiability that could never run with larger conditional expressions?

It would be useful, but checking type is not the same as comparing values.
Type checking is implemented usually by assigning types to parameters/variables and adding --!strict to the start of the script file which will enable “highlighting” wrong types. Something like:

--!strict

local function myFunction(num: number, num2: number) : ()
    -- do something
end

myFunction("hi", 5) -- will result in a TypeError and will be highlighted red

And I really don’t see a room to add “comparative” checks except within the function body itself, like:

local function myFunction(num1: number, num2: number) : ()
    assert(num1 < num2, "num1 must be less than num2")
end

It also feels quite right for me to do it that way.

I kinda get it now, thanks for the help

Comparative checks as part of the type checking of parameters would be a nice feature (?)

It would be a nice feature indeed.

1 Like