DIPLOMA PROBLEMS

Given n people, what is the probability that your diploma's number is within 1 of the correct value?So if you are number 5, get either 4, 5 or 6; if there are n people we say 1 and n are adjacent. Answer is 3/n if n ≥ 3. Next: what is the expected number of people to get a diploma within 1 of theirs?
Linearity of expectation: Let X_i be 1 if person i gets a diploma within 1 of theirs, and 0 else.
If X is the number of people with a diploma within 3, LinguisticAssistant
So E[X] = Sum E[X_i] and E[X_i] = 1 * 3/n + 0 * (n-3)/n = 3/n
SO E[X] = Sum_{i = 1 to n} 3/n = 3

threegrad[printcheck_,numiter_, n_] := Module[{},
count = 0;
list = {};
For[j = 1, j ≤ n , j++, list = AppendTo[list,j]]; (* creates list of numbers *)
For[i = 1, i ≤ numiter, i++,
{
newlist = RandomSample[list,n];
If[i == 1  && printcheck == 1, Print[list, " ", newlist]];
currentcorrect = 0; (* initialize to zero, no one is within 1 yet *)
For[d = 1, d ≤ n, d++, If[Abs[newlist[[d]] - d] ≤ 1, currentcorrect = currentcorrect +1]]; (* d statement ends *)
(* have to handle wrap-around *)
If[newlist[[1]] == n, currentcorrect = currentcorrect + 1];
If[newlist[[n]] == 1, currentcorrect = currentcorrect + 1];
If[i == 1 && printcheck == 1, Print["Number of matches is ", currentcorrect]];
count = count + currentcorrect;
}]; (* end of i loop *)
Print["The average number observed that are within 1 is ", 1.00 count / numiter];
] (* end of module *)

Timing[threegrad[0,100000,10]]

Now consider a success if diploma is the same parity as yours; even-even or odd-odd; not even-odd or odd-even. Now what is the expected number of successes?
Recursion: if n =1 expected number of successes is 1
If n=2 expected number of successes is 1 = 2 * 1/2 + 0 * 1/2
If n = 3:  123=132=213=231=312=321     (3+1+1+1+1+3)/6 = 10/6 = 5/3
a(2n) = 1/2 (1 + a(2n-1)) + 1/2(0 + a(2n-2))
a(2n + 1)  = ???

paritygrad[printcheck_,numiter_, n_] := Module[{},
count = 0;
list = {};
For[j = 1, j ≤ n , j++, list = AppendTo[list,Mod[j,2]]]; (* creates list of numbers *)
For[i = 1, i ≤ numiter, i++,
{
newlist = RandomSample[list,n];
If[i == 1  && printcheck == 1, Print[list, " ", newlist]];
currentcorrect = 0; (* initialize to zero, no one is within 1 yet *)
For[d = 1, d ≤ n, d++, If[newlist[[d]]== Mod[d,2], currentcorrect = currentcorrect +1]]; (* d statement ends *)
If[i == 1 && printcheck == 1, Print["Number of matches is ", currentcorrect]];
count = count + currentcorrect;
}]; (* end of i loop *)
Print["The average number of same parity is ", 1.00 count / numiter];
] (* end of module *)

Timing[paritygrad[1,10000,10]]