Math.random() for numbers sub 1

I’m trying to make a system that will make a TextLabel spawn at a set y point but a random x point.

I tried using math.random() like this, but this obviously yields an error. Since there must be a way to do this, as I have seen randomly generated ui on multiple occasions, I am thoroughly frustrated as to why I can’t crack this…

I tried making a seperate random number variable as shown above, and then plugging it in to the Udim to achieve a decimal, but that doesn’t work. Problem is, all Udim values are <= 1.

Anyways, thanks for reading/answering

2 Likes

try maybe this?

local numX = math.random(1, 9)
clone.Position = UDim2.new(numX, 0, 0, 0)

I thought of this but the problem is all Udim values must be lower than 1, and in this case it will yield a number 1-9.

Oh wait, try this

local numX = math.random(1, 9)
clone.Position = UDim2.new(0, numX, 0, 0)

Udim also has a UDim2.fromOffset(x, y) and a UDim2.fromScale(x, y) constructors you can use. I’m guessing you want a random offset so you can just use it directly like this:

local numX = math.random(1, 9)
clone.Position = UDim2.fromOffset(numX, 0)

Divide the math.random by 10. For example, if it generates 4, dividing it by 10 converts it to 0.4. Or, if you have a number range of 1-100 you can divide by 100

Very smart thank you very much

1 Like

Or just use math.random without arguments.

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