Should math.random not be used for new work?

Hey, so I’m aware of the new random class that was added recently, where you can create new instances of random, and use the methods upon it, like so:

local generator = Random.new()

print(generator:NextInteger(1, 2))

Does that mean math.random is deprecated or shouldn’t be used for new work? I sometimes use it for cases where I don’t want to create a new instance of it, so I just call random instead for simplicity.

Thanks. :slight_smile:

I have never used random.
However, I do not think math.random() is deprecated, and i’ve never had any issues with it.

2 Likes

The math.random function is perfectly fine to use. When you’re just making one or two calls, it seems a bit silly to make a new Random object.

Edit: I’d hardly call it recent, by the way. It was added in 2017.

5 Likes

Also they use the same algorithm now.

The Random class does have its benefits though

  • Seeds are exclusive to specific instances (math.randomseed affects all scripts)
  • You can generate non-integers without needing to do any extra math
3 Likes

math.random() completely works, however it is capped at the signed 32 bit integer limit (2.14Billion), which the new Random class isn’t. Random:NextInteger() is capped at the signed 64 bit integer limit (9.22 Quintillion). and Random:NextNumber() is capped at the float64 limit (1.8*(10^308)). So i suggest using Random:NextNumber() and then using math.round to round up the Number (since double values can store a lot of decimals and blow your head)

1 Like