Can someone explain what the arguments of math.random() does?

So i’m trying to figure out what it means when one argument is input into the paranthesis of math.random. I looked it up on the devhub and I don’t understand the definition provided:

“This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. (No guarantees can be given for its statistical properties.) When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].”

Can someone explain it in simpler terms, thanks.

It’s simple but it was initially confusing for me as well.
Math.random(1,5)
This just randomly returns a number between 1 & 5
@Deathlios am not sure what that does but I think it randomly generates a number from 0-5

math.random() returns a number in the interval [0, 1), so including 0 and excluding 1. So it can generate 0, up to 0.9999999999999… but never 1.

math.random(x) returns an integer in the interval [1, x] so includes 1 and x.

math.random(x, y) returns an integer in the interval [x, y] so includes x and y.

5 Likes

I get that, but if you put “math.random(5)”? What does that do?

1 Like

I like how they use the “pseudo-random” tag… xD I always think, how random this function really is?

Giving one value… idk what it does, but I kinda dont find a situation to use that :v

This was explained right in the post above your own. A single value defines an inclusive-maximum between that number and 1. So math.random(5) generates between 1 and 5 including 1 and 5.

1 Like

oooooh okay I get it now. Thanks

Thanks for the help again fam. Last time you helped me on the script highlight problem I had lol.

“pseudo” means fake, so “pseudo-random” quite literally means “fake random”, and for a good reason. From a computational perspective, true randomness is not feasible. math.random is what is known as a pseudo-random number generator (PRNG). Things can be done to make numbers look random, however the next number is usually based on the previous number.

I didn’t really understand, if you meant giving 1 argument to math.random, this is useful for when you want the minimum number to be 1 but the maximum something greater.

3 Likes

I know that, thats why I said it :3

For me, I have very bad practices, I use math.random a lot, I gave 2 values always, maybe I’ve never needed a value between those quantities, its just I need to code more in order to find it useful (for me as I said)

1 Like