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>Let's say that your new point (x, y) is on a cercle of radius 10 and center (x0, y0). The random component is the angle.</p>

<pre><code>import math as m
# radius of the circle
r = 10
# create random angle and compute coordinates of the new point
theta = 2*m.pi*random.random()
x = x0 + r*m.cos(theta)
y = y0 + r*m.sin(theta)
# test if the point created is in the domain [[0,50], [0, 50]] (see comments of PM2Ring)
while not ( 0&lt;=x&lt;=50 and 0&lt;=y&lt;=50 ) :
    # update theta: add pi/2 until the new point is in the domain (see HumanCatfood's comment) 
    theta += 0.5*m.pi
    x = x0 + r*m.cos(theta)
    y = y0 + r*m.sin(theta)
</code></pre>

python random