Perhaps this is expected behavior, but I was kind of surprised by the following:
a[k_] := RandomInteger[{-1, 1}];
f[n_] := Sum[a[m], {m, n}]
Table[f[n], {u, 5}]
(*{0, n, -n, -n, -n}*)
I was expecting it to simply give up, and return Sum[RandomInteger[{-1, 1}], {m, n}]
. Instead, it looks like when doing the symbolic sum, Sum
considers a[m]
to be a constant that is independent of m
, pulls it outside of the sum, and then evaluates Sum[1, {m, n}]
times it.
In contrast, using an explicit value of $n=5$ gives the expected results:
Table[f[6], {u, 5}]
(*{-2, 2, -5, 0, 1}*)
I looked in the Possible Issues
section of Sum
and didn't see anything addressing this. Is there a reason for why Sum
considers a RandomInteger[]
term in the summand to have the same value for all terms?