Testing whether or not the sequence is "random" can be fairly difficult, since a process can be random in many different ways. To make the problem more manageable, you could test the Hypothesis:
$$H_0: X_1, X_2, \cdots X_n \stackrel{iid}{\sim} Bernoulli(\theta)$$
From here, there are several test statistics that you could incorporate to test whether this specification is reasonable. A few choices are listed below. You can add to the list to fit your specific goal.
\begin{align*}
T_0 &= \text{ longest streak of $0$'s} \\
T_1 &= \text{ longest streak of $1$'s} \\
T_2 &= \text{ number of state changes} \\
T_3 &= \text{ auto-correlation of lag 1}
\end{align*}
Rather than trying to find the sampling distributions of these test statistics under $H_0$, we can test these hypotheses empirically, using the most likely value of $\theta$ (Andrew Gelman does this in Bayesian Data Analysis).
I have simulated a series of $1000$ coin flips from a model which is NOT iid Bernoulli (although it is still random). We could test these hypotheses in R as follows.
- The proportion of heads in my sample is $\hat\theta = 0.38$
- $T_0 = 19$
- $T_1 = 11$
- $T_2 = 376$
- $T_3 = 0.198$
Now we can test these statistics against their empirical null as follows. Note that I wrote separate functions for calculating the test statistics above.
M <- 2000
T0 <- T1 <- T2 <- T3 <- rep(NA, M)
for(m in 1:M){
#Simulate data according to the Null
x <- rbinom(1000, 1, 0.39)
#Compute test statistics
T0 <- get_T0(x)
T1[m] <- get_T1(x)
T2[m] <- get_T2(x)
T3[m] <- get_T3(x)
}
And now we plot the empirical null distributions along with the observed value from the data. Clearly the observed data doesn't seem to match the Null distribution here... indicating that our null hypothesis is probably incorrect.
