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 you have a point <code>(x, y)</code></p>

<ol>
<li><p>create another random point <em>anywhere</em> on the plane: <code>(x1, y2) = (random(), random())</code></p></li>
<li><p>take the vector from your point to the new point: <code>(vx, vy) = (x1-x, y1-y)</code></p></li>
<li><p>get the length <code>l</code> of the vector: <code>l = sqrt(vx * vx + vy * vy)</code></p></li>
<li><p>use <code>l</code> to normalise the vector (so it has a length of 1): <code>(vx, vy) = (vx / l, vy / l)</code></p></li>
<li><p>make the vector 10 steps long: <code>(vx, vy) = (vx * 10, vy * 10)</code></p></li>
<li><p>add it to your original point to get to the desired point: <code>(x1, y2) = (x + vx, y + vy)</code></p></li>
</ol>

<p>voilÃ¡ :)</p>

python random