Find closest value in a list

I have 2 int, lets call them x and maxoffset, and a List of ints called y. I have a list of y (lets say its 2 long). 1 of the values is 0 and the other is -4. On x I have a int that I can change, and I want it to choose the closest number if the distance is less than or equal to maxoffset. This means if maxoffset is 2 and my number that I wrote for x is -2 then both 0 and -4 have a equal chance of being chosen since they are equal distance. if I choose -1 then the value 0 has a chance of being chosen, but -4 has no chance because -1 is 3 away from -4, which is more than the maxoffset. If that -4 was a -3 then since 0 is closer to -1 than -3 is, its more likely that 0 gets chosen, but the -3 can still get chosen since its within the max offset. Keep in mind this is just an example and I will end up having the list be much longer.

EDIT: Figured out a way to make the offset work as intended, I just now need to find out a way for weighting so the one closer to the variable is more likely to be chosen instead of equal chance

public int ClosestToX(int x, int maxoffset, List y) {
int Index = -1;
int Offset = maxoffset + 1;

     for (int i = 0; i < y.Count; i++) {
          int Distance = Mathf.Abs(x - y*);*

if (Distance <= maxoffset && Distance < Offset) {
Index = i;
Offset = Distance;
}
}
//Will return -1 if there is no number within the maxoffset
return y[Index];
}
You may have to change some variable names to keep is as a function, or if you take the code out of the function and put it straight in you can use y[Index] to get the closest number.