How do I get random positions of outside a given box?

Randomly in my world, I have boxes. I want to be able to get a random point outside of the box.

image

For example, if a 4x4x4 cube is at 0,0,0, I would generate a random position that is not inside that 4x4x4 area at 0,0,0 but relatively close. I would want outputs of a position at like 0.5, 1, 2.5 or -0.5, 2.5, -0.5 as these are points outside of the box.

How far away from the box doesn’t really matter, but I would like it to be near the surface of the box and absolutely not inside of it.

I’m not looking for full code, but any pointers would be nice. I have no idea where to start.

I would do something like getting a math.random position around the outside area of the box, and adding an if statement that says if the given point is inside this specific area(the 4x4 box), try again until it is not.

1 Like

I thought of this but I don’t want to waste time by retrying if it’s inside the box.

I’m sure there’s math to make it always outside but I just don’t know where to start with it.

i mean, it wouldn’t really be a noticeable amount of time, since the computer would process it as nil in less than a second.

1 Like

If the box is hard coded you could try to split the desired volume into boxes and doing some math to calculate the volume of each to then do a weighted random to choose in which box it should land and then just do 3 rands for a random value inside the selected box

they are not hard coded unfortunately. they are boxes in random sizes and in random positions. orientation is always 0,0,0 however.

id like to be able to upscale it massively, like getting a new random position a thousand times per second.

what exactly are you trying to do with the code? Is it for a particle effect or something?

That would be a lot harder, but the math would be possible to automate.