I want to generate the random sequence defined by $a_n = a_{n-2} + \beta(n)a_{n-1}$ where $\beta$ takes the values $\pm1$. My attempt is:
RecurrenceTable[{a[n] == a[n - 2] + (-1)^(RandomInteger[{0, 1}])*a[n - 1],
a[0] == 1, a[1] == 1}, a, {n, 0, 10}]
But of course, RandomInteger
only computes on the first loop, and then saves that value for all subsequence computations. I found this solution here:
r[n_] := RandomInteger[{0, 1}];
rfib[0] = 1;
rfib[1] = 1;
rfib[n_] := rfib[n] = rfib[n - 2] + (-1)^r[n]*rfib[n - 1];
Table[rfib[i], {i, 0, 10}]
However, I am wondering if my original intuition can be salvaged; i.e., is there a way to use RandomInteger
inside RecurrenceTable
and get a new integer each time?
Edit:
After posting this and looking for something else, I found this post. From this, I can write the code as
rr[n_?NumericQ] := RandomInteger[{0, 1}];
RecurrenceTable[{a[n] == a[n - 2] + (-1)^(rr[n])*a[n - 1],
a[0] == 1, a[1] == 1}, a, {n, 0, 10}]
as I originally wanted to.