As it is, this function generates a few random numbers and then stops. If you uncomment the commented lines, it creates a copy of the input with two random elements swapped (or loops forever if the input has only 1 element, or raises an exception if the input is empty). Here's a line-by-line breakdown:
# The default argument here is useless.
def Recocido(tour1 = []):
# Make a copy of the list.
tour1 = tour1[:]
# Pick a random index.
izquierda = random.randrange(len(tour1))
# Unnecessary.
derecha = 0
while(True):
# # Pick another random index
derecha = random.randrange(len(tour1))
# If it's not the same index you picked the first time,
if (derecha != izquierda):
# swap the elements of the copy at the two indices,
tour1[izquierda], tour1[derecha] = tour1[derecha], tour1[izquierda]
# and stop looping.
break
return tour1
(I hope this is only one part of your simulated annealing program, because this is not simulated annealing. There is no function to optimize, no cooling schedule, and no probabilistic rejection of state changes.)
If you're trying to write a function that returns a copy of an input list with two random elements swapped, you could clean it up as follows:
# No default argument - you'd never want to use the default.
def with_random_swap(lst):
# Use random.sample to pick two distinct random indices.
index1, index2 = random.sample(xrange(len(lst)), 2)
copy = lst[:]
copy[index1], copy[index2] = copy[index2], copy[index1]
return copy