I have a list where I am selecting a number at random. I now want to choose the closest integer in the list to the number I have selected. Below is what i have so far:
from random import choice
a = [0,1,2,3,4,5,6,7,8,9]
r = choice(a)
So for example if r = 9, the nearest neighbor would be 8. For r=7 it could be either 6 or 8. Which of the two closest isn't particularly important, so long as it is a neighboring value.
Aucun commentaire:
Enregistrer un commentaire