[Bugfix] fixed distance formula

The distance formula calculation in gen_within_distance did not square
the distance.