Question about math.random

Hi! I wanted to know how math.random works. I know that you can supply 2 integers and the random will choose any number in between them, but I wanted to know, is there any way to make it return a float? Right now, all math.random’s I’ve used always returned an integer, but I need it to return a float. So basically, for the first scenario, I want math.random to return a float in between 0 and 1 and for the second, I want it to return an integer between 1 and 10. Is there any way to achieve this? Thank you!

I forgot to add, I have tried supplying the two variables as floats, for example: math.random(0.0, 1.0), but it still comes out as either 0 or 1 but nothing in between

I know this might be hacky, but you could do math.random(0, 100)/100

2 Likes

Thank you! This seems like it should work but I wanted to know whether there was a built in function for this as I’m sure there must be some way to get a random float. Thanks!

1 Like
Random.new():NextNumber()
3 Likes

I’m sorry, I didn’t understand what you wrote. Is there a way to make this return a float in between 0 and 1? If so, could you tell me how? Thanks!

Random.new() makes a new Random object with a random seed and NextNumber() generates a random float from the Random object with the Random object’s seed. (The float is between 0 and 1 by the way)

1 Like

Thank you! One last thing, is it possible for me to see an example? I didn’t really understand what a “seed” was. If I wrote something like this:

local rand = Random:NextNumber(0,1)
print(rand)

then will it print a float in between 0 and 1? Thanks!

Hey AAD232007!

You should check out Random.new(). It works similarly and provides a float!

local random = Random.new(os.time())
local minimumnumber = 0
local maximumnumber = 1
--
local float = random:NextNumber(minimumnumber,maximumnumber)
print(float)
2 Likes

Thank you for your reply! I’m very sorry, but I have one final question. Why did you have to declare the variable “random” as
Random.new(os.time()) ?
I understand the bit where you create the float variable, I just have no clue what the “random” variable is used for. Thanks!

Hey AAD232007!

The parameter for Random.new() is a pseudo-seed number that it uses to generate the numbers. I use os.time() because it will never be the same!

1 Like

You could just use math.random() without no arguments to make a random float between 0 and 1

1 Like

Thanks! So basically, this is just used to create the float? Otherwise, it doesn’t matter what it is? Thank you!

Hey AAD232007!

Yep, that is used to generate the float!

1 Like

You must first create a Random Object. It accepts a number as a parameter, which will be used as a seed. If no seed is provided, it comes up with a random one. Here is an example of what you are trying to achieve.

local Random = Random.new() — you can put something here as a parameter like suggested by above posts, but it really is unnecessary. If you want, you could put os.clock() as a parameter.

local randomFloat = Random:NextNumber(0,1)

print(randomFloat)

Using Random.new is better than math.random because unlike math.random, it provides pseudo-random numbers. This means it is essentially more random.

If you want to generate a random interference, use the following:

local Random = Random.new()

local randomInt = Random:NextInterger(0,100)

print(randomInt)

1 Like

Thank you! This will be useful for now, but I wanted to use this for the future as well, and I might not always want to make a random number in between just 0 and 1. Thanks!

Thank you for the quick reply! After reading yours, @arccitecc , and @heII_ish 's replies, I finally understand how to use Random:NextNumber(). Thank you for your help!