Drawing random smooth lines contained within a square
Random walks "wander all around" by definition. So you have to twist the odds of the walk staying in the area you want.

It would be much simpler if you'd just make an attractor point at the center, that would move each point infinitesimally (some floating point value between 0 to 1 or sqrt(2) pixels) towards itself at each step.

Another option would be to make the edges exponentially hard to reach. I'm thinking of an Exponential_map with a really steep slope at the edges and otherwise near linear curve. also see Gamma curve. So essentially map an "infinite" area to the restricted space.

Third option would be to think about how magnetism works. I mean, a magnet can't be any more magnetized to a certain direction after all it's particles are magnetized towards that direction. And the magnetization takes exponentially stronger field all the way towards that state. I've written a "tape compression" algorithm with Numpy using that idea.