JAA JAA - 2 months ago 10
Python Question

How can I generate a random point (x, y) 10 steps apart from y0(a, b) in xy-plane?

I have generated a random point named

y0=(a,b)
in xy-plane , How can I generate another random point
(x,y)
10 steps apart from
y0
?

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

My attempt(sometimes gives wrong result).

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)

JAA JAA
Answer

this code generate a random point xy-plane named y0 then generate another point x0 10 steps apart from y0 in taxi distance .

------- begining of the code-------- import random y0=(random.randint(0,50),random.randint(0,50))

  while True:

      y=random.randint(0,50)

      x=(10 -abs(y-y0[1]))+y0[0] 
      if (abs(x-y0[0])+abs(y-y0[1]))==10:
         x0=(x,y)
         break
Comments