How to write a edge loop script for Blender?

I would like to write a script which selects a random polygonial loop on subdivided object.

For example:

There should be option if we allow multiside loop:

and multiple loops on one side:

Also, there should be parameters which control how random or wide the loop is. So,we can choose simple one (square loop) or complex ones (like in the images).

Could you tell me how to write this?