Math.random() returning higher or lower value than it's supposed to

I am trying to make some cabinets spawn randomly in a random generated maze but for some reason math.random() returns me a higher value than it is supposed to. For example, let’s say the value should be inbetween 59.75 and 60.25, but instead of that, it returns 61 or 59 and I have no idea why.
Here is the portion of code:

local xPosition = location.Position.X
local zPosition = location.Position.Z
local yRotation = location.Rotation.Y

local xSize = location.Size.X
local zSize = location.Size.Z

local x = math.random(xPosition - xSize / 2, xPosition + xSize / 2)
local z = math.random(zPosition - zSize / 2, zPosition + zSize / 2)
local y = script.Cabinet.Frame.Size.Y / 2 + script.MazeCell.Floor.Size.Y / 2

local rotation = math.random(yRotation - 30, yRotation + 30)

local cabinet = script.Cabinet:Clone()
cabinet.Parent = workspace.Maze.Shelves

local cframe = CFrame.new(x, y, z) * CFrame.Angles(0, math.rad(rotation), 0)
cabinet:SetPrimaryPartCFrame(cframe)

If you know another method of how I can do or fix this please let me know.

2 Likes

Try making it 5975 and 6025 and dividing the result by 100

1 Like

Thanks for the response but I get the same results as before.

1 Like

Where exactly is issue, can you quote?

So I have a part in which I want to spawn a cabinet, but when I try calculating the Z position it just gives me a value that’s not inside the part. It’s the exact same calculation I used for X but X works fine.

1 Like

Try printing the values and make sure it’s in the preferred range. Also make sure the part is anchored

I’ve printed them out but math.random() just gives me something out of range.

1 Like

math.random only takes in integers, not floats. As @RoninQV mentioned you should convert the float into an integer by multiplying the float by a certain amount, then dividing after. For example if you want a number between 0.1 ( x ) and 1 ( y ), firstly you should multiply each probable side by 10 to make each side an integer. Now we have 1 ( x ) and 10 ( y ). Now you use math.random() to calculate a random number between x and y as such:

math.random(x, y)

Now that you computed a random number between the two arguments, you now need to divide the computed number by the same number you multiplied with:

math.random(x, y) / 10

So the final formula is as such:

local x: number = 0.1
local y: number = 1

math.random(x * 10, y * 10) / 10

So in your case you should do:

local x0: number = (xPosition - xSize / 2) * 100
local x1: number = (xPosition + xSize / 2) * 100

local z0: number = (zPosition - zSize / 2) * 100
local z1: number = (zPosition + zSize / 2) * 100

local x: number = math.random(x0, x1) / 100
local z: number = math.random(z0, z1) / 100

Thanks, I didn’t know it only takes integers.

2 Likes

You can just use Random.new():NextNumber() which should return decimal values.

local rmd = Random.new()

local number = rmd:NextNumber(0, 1)

So you dont really need to multiply and divide as much as you think you need to.

And also, it isnt entirely true that math.random() returns an integer, when you use it without any arguments. It gives you a decimal value between 0 and 1.

Floats in Lua are also known as “Doubles”

1 Like

Pardon my inaccuracies. You are indeed correct, math.random() with no given arguments does project a random number between 0 and 1. When I said ‘math.random only takes in integers’ I specifically meant the arguments it takes in must be integers. I should’ve probably been more clear, apologies. My bad for also calling ‘doubles’ ‘floats’. I program in python quite a bit so that again is my bad.

I wasnt talking to you about that, but it seems that the post I was referring to was edited.

It doesnt matter, people call it Floats anyway, but Its term in Lua is “Doubles” which is what I was saying

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.