Random negative number

Hi,

I am trying to choose a random negative number between -10 and -25 but getting the error below and not certain why.

local randX = math.random(-10, -25)

Error
Players.CanterCrow.PlayerScripts.GUIAnimation:14: invalid argument #2 to ‘random’ (interval is empty)

1 Like

Try this:
-math.random(10, 25)

If you’re starting from zero, you can also use something like this:
-math.random() * 10

Edit: the solution below works as well, my preferences is just to add the - before the math.random function. Either way works.

When dealing with negatives, the arguments are reversed, as the lowest number always comes first. -25 is lower than -10.

3 Likes

Switch the order of the numbers to math.random(-25, -10)

5 Likes

Flip the 2 values around lol

local randX = math.random(-25,-10)

math.random requires 2 arguments, as the first argument has to be less than the second argument

3 Likes

But the first arguement is already bigger, isn’t -10 greater than -25?

Yes - that’s the problem.

math.random(Low, High)

The lower number always comes first, i.e math.random(1, 5), you wouldn’t put the 5 first.

Edit: did not see the typo on the post you were referring to, please ignore my post :slight_smile:

2 Likes

I made a typo whoops, sorry (I meant to say the first argument has to be less than the second argument)

Incorrect. math.random can take no or one argument.

  • math.random() with no arguments, generated a random number between 0 and 1
  • math.random(x) with 1 argument is just math.random(1, x) until Lua 5.3 if x is 0 (as that’ll generate a random number from the minimum value to the maximum value a signed 64-bit integer can have)
3 Likes