HELP! - containing random motion

Hi all, if anyone could help me with this I’d greatly appreciate it. I’m experimenting with random brownian motion and it works great. Code below:

    goto=Math.random()*Math.PI*2
x=x+r*Math.cos(goto);
y=y+r*Math.sin(goto);
this.lineTo(x,y);

so what this gives me is a great random little scribbler that I love. What I can’t get it to do is to follow an “approximate path” while moving randomly. I.e. moving randomly within a confined space. I don’t want to use the traditional bouding box idea b/c the shape I’d like to confine it to is more complex. What I need is more of a path solution, or a bounding box that is a complex shape. Any ideas anyone?

Best regards,
zf.