Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is it showing empty interval on math.random???

I have a map located at 0, 0, 0 with dimensions of 896, 896, 896. Let's say I want to generate a part with a random x and y position on said map. So I do:

local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
part.Position = Vector3.new(math.random(896, -896), math.random(125, 150), math.random(896, -896))

However, running this code gives the following error: invalid argument #2 to 'random' (interval is empty)

As you can see, however, the interval is not empty. Any help would be appreciated!

3 comments

0

You're putting the negative and positive numbers in the wrong spot. Smaller numbers go on the left side like on a number line. Swap the negative number and the positive number. Also, you may want to half the number of 896 since you don't want the part to spawn outside of the map.

0

Use math.random(-448,448) instead. If the bottom left corner of the map were located at 0,0,0 then you'd use 0,896. Since the center of the map is at 0,0,0, use half of the map's size.

0

Thx! Also leave it as an answer so I can accept :P

1 answer

✓ Accepted answer

#math.random math.random has 2 arguments. The first is the lesser number and the second is the greater number. The random number generator picks any integer in between those 2 numbers (unless the distance between the two numbers is less than or equal to 1). The first argument MUST be less than the second one.


Be sure to half the size of your map when using these calculations, so the part doesn't spawn outside the map. The absolute value of -448 and the absolute value of 448 add up to a full 896 studs as you can see here. It's simply because the center of the map is at the origin. If the bottom left corner of the map was at the origin, you'd use 0, 896. If the top right corner is at the origin then you'd use -896, 0. Regardless of what you do, the sum of the absolute value of both dimensions should equal the total length or width of the map.

math.random(-448,448)
print(math.abs(-448)+math.abs(448))
--896


####Hope it helps! -LordDragonZord

1 comment

0

Takes me time to write answers like these, my bad.

This discussion is preserved as it was. New answers, comments, and votes are closed.