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><code>abs(x)+abs(y)=10</code> defines a <a href="http://www.wolframalpha.com/input/?i=plot+abs(x)%2Babs(y)+%3D+10" rel="nofollow">square</a>, so all you need to do is pick a random value along the perimeter of the square (40 units long), and map that random distance back to your x,y coordinate pair.</p>

<p>Something like (untested):</p>

<pre><code>x = random.randint(-10,9)
y = 10 - abs(x)
if (random.randint(0,1) == 0):
    x = -x
    y = -y
x = x + y0[0]
y = y + y0[1]
x0=(x,y)
</code></pre>

<p>Clipping the <code>x</code> range that way ensures that all points are picked uniformly.  Otherwise you can end up with (-10,0) and (10,0) having twice the chance of being picked compared to any other coordinate.</p>

python random