How can I generate a random point (x, y) 10 steps apart from y0(a, b) in xy-plane?
<p>I have generated a random  point named <code>y0=(a,b)</code> in xy-plane , How can I generate another random point <code>(x,y)</code> 10 steps apart from <code>y0</code>? </p>

<p>note: by 10 steps apart from the firt point I don't mean the Euclidean distance. I mean the number of steps on lattice between the two point (a,b) and (x,y) which is given by |x-a|+|y-b|=10</p>

<p>My attempt(sometimes gives wrong result).</p>

<pre><code>import random
y0=(random.randint(0,50),random.randint(0,50))# here I generated the first point.
y=random.randint(0,50)

# I used the formula  |x-a|+|y-b|=10.
x=(10 -abs(y-y0[1]))+y0[0]  or x=-(10 -abs(y-y0[1]))+y0[0]

x0=(x,y)
</code></pre>

<p>So, you got the formula <code>d=d1+d2=|x-x0|+|y-y0| , for d=10</code></p>

<p>Let's examine what's going on with this formula:</p>

<ul>
<li>Let's say we generate a random point P at (0,0) </li>
<li>Let's say we generate <code>y=random.randint(0,50)</code> and let's imagine the value is 50. </li>
</ul>

<p>What does this mean? </p>

<p><code>d1=|x-p[0]|=50</code> and your original formula is <code>d=d1+d2=|x-x0|+|y-y0|</code>, so
that means <code>d2=|y-y0|=10-50</code> and <code>d2=|y-y0|=-40</code>. Is this possible? Absolutely not! An absolute value |y-y0| will always be positive, that's why your formula won't work for certain random points, you need to make sure (d-d1)>0, otherwise your equation won't have solution.</p>

<hr>

<p>If you wanted to consider Euclidean distance you just need to generate random points in a circle where your original point will be the center, something like this will do:</p>

<pre><code>import random
import math


def random_point(p, r=10):
    theta = 2 * math.pi * random.random()
    return (p[0] + r * math.cos(theta), p[1] + r * math.sin(theta))
</code></pre>

<p>If you draw a few random points you'll see more and more how the circle shape is created, let's try with N=10, N=50, N=1000:</p>

<p><a href="http://i.stack.imgur.com/CnY7e.png" rel="nofollow"><img src="http://i.stack.imgur.com/CnY7e.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/KrVxJ.png" rel="nofollow"><img src="http://i.stack.imgur.com/KrVxJ.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/9L9pI.png" rel="nofollow"><img src="http://i.stack.imgur.com/9L9pI.png" alt="enter image description here"></a></p>

<p>Now, it seems you need the generated circle to be constrained at certain area region. One possible choice (not the most optimal though) would be generating random points till they meet those constraints, something like this would do:</p>

<pre><code>def random_constrained_point(p, r=10, x_limit=50, y_limit=50):
    i = 0
    MAX_ITERATIONS = 100

    while True:
        x0, y0 = random_point(p, r)

        if (0 &lt;= x0 &lt;= x_limit and 0 &lt;= y0 &lt;= y_limit):
            return (x0, y0)

        if i == MAX_ITERATIONS:
            return p

        i += 1
</code></pre>

<p>Once you got this, it's interesting to check what shape is created when you increase more and more the circle radius (10,20,50):</p>

<p><a href="http://i.stack.imgur.com/XHgVs.png" rel="nofollow"><img src="http://i.stack.imgur.com/XHgVs.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/hUZDr.png" rel="nofollow"><img src="http://i.stack.imgur.com/hUZDr.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/pXYdB.png" rel="nofollow"><img src="http://i.stack.imgur.com/pXYdB.png" alt="enter image description here"></a></p>

<p>As you can see, your generated random constrained points will form a well_defined subarc.</p>

python random