How to make it so an object going random directions always stays within the parents range?

Basically this: I would like to make it so with an Empty object, the object adding the particles stays within the parent objects range and doesn’t veer off like crazy.

So far, I have sort of achieved the random part of the movement and parent detection using Pythons random module and if statements (code):

import bge
from bge import logic as logic
import random
import math

cos = math.cos
sin = math.sin

def main(cont):

# get owner
own = cont.owner

# randomise
rand = random.randint(own['randomin'],own['randomax'])

# position
randx = own['randomx']
randy = own['randomy']
randz = own['randomz']
own['randomx'] = rand
own['randomy'] = rand
own['randomz'] = rand
    
if own.parent:
    rx = own.parent.worldPosition[0] * own.worldPosition[0] == randx
    ry = own.parent.worldPosition[1] * own.worldPosition[1] == randy
    rz = own.parent.worldPosition[2] * own.worldPosition[2] == randz
else:
    rx = int(rand(-randx,randx) * own.worldPosition[0]) * randx
    ry = int(rand(-randy,randy) * own.worldPosition[1]) * randy
    rz = int(rand(-randz,randz) * own.worldPosition[2]) * randz

But it just veers off. I have tried to solve it in different ways, but it keeps veering off. I am also using game properties for finer control too.