I need help with this problem


Generating Random Cities

Write a function called generate_cities(min_x, max_x, min_y, max_y, n, distance) that generates a list of cities with random coordinates. The parameters represent the following:

* min_x, max_ x: the minimum and maximum values for the x coordinate of each city
* min_y max_ y: the minimum and maximum values for the y coordinate of each city
* n: the number of cities to be generated (that is the number of [x, y] lists)
* distance: the minimum distance allowed between any two cities

Here is a brief description of how you can generate your cities while ensuring that they are not too close to each other

This is the algorithm that I have to follow
repeat n times
generate random coordinates for new_city
too_close = True
while too_close == True
too_close = False
for each city in the city list so far
if the new_city < distance from city
too_close = True
generate new random city point
break
add new_city to the results list

This is as far as I got:

def generate_cities(min_x, max_x, min_y, max_y, n, distance):
for steps in range(N):
random_coordinates_x = random.randint(min_x, max_x)
random_coordinates_y = random.randint(min_y, max_y)
new_city = [random_coordinates_x, random_coordinates_y]
print new_city

where's the code to support the rest of the algorithm?

It looks like it might be useful to write a function to calculate the distance between two cities (or two coordinates).

This article has been dead for over six months. Start a new discussion instead.