Wolfram Research

Function Repository Resource:

SerialRandomnessTest

Source Notebook

Conduct an empirically derived test that assesses randomness using the frequencies of serial combinations of 0s and 1s

Contributed by: Emmy/Noah Blumenthal
noahb320@gmail.com
emmyb320@bu.edu

ResourceFunction["SerialRandomnessTest"][sequence]

conducts an empirically derived test using the frequencies of serial combinations of 0s and 1s in sequence and returns an associated p-value.

ResourceFunction["SerialRandomnessTest"][sequence,"properties"]

conducts an empirically derived test and returns the specified property.

Details and Options

Properties include:
"TestStatistic" returns the test statistic
"PValue" returns the p-value associated with the test
The test statistic is generated by creating a chi-square-like statistic that compares the frequencies of subsequences of 0s and 1s of length two to the expected frequencies.
The test works only for sequences of 0s and 1s.
Results are generally invalid for sequence lengths short than 100.

Examples

Basic Examples

Generate a sequence of random integers and apply a runs-based test:

In[1]:=
sequence = RandomInteger[{0, 1}, 1000];

Visualize the sequence:

In[2]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[2]=
In[3]:=
ResourceFunction["SerialRandomnessTest"][sequence, "TestStatistic"]
Out[3]=
In[4]:=
ResourceFunction["SerialRandomnessTest"][sequence, "PValue"]
Out[4]=

Applications

Reject a non-random sequence:

In[5]:=
sequence = Flatten@Table[subsequence, 20];
In[6]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[6]=
In[7]:=
ResourceFunction["SerialRandomnessTest"][sequence, "TestStatistic"]
Out[7]=
In[8]:=
ResourceFunction["SerialRandomnessTest"][sequence, "PValue"]
Out[8]=

Test the randomness of rule 30:

In[9]:=
rule30seq = CellularAutomaton[30, {{1}, 0}, {100000, 0}]\[Transpose][[1]];
In[10]:=
ArrayPlot[Partition[Take[rule30seq, 1000], 16]\[Transpose], Sequence[
 ColorFunction -> "CandyColors", ImageSize -> Large]]
Out[10]=
In[11]:=
ResourceFunction["SerialRandomnessTest"][rule30seq]
Out[11]=

Possible Issues

SerialRandomnessTest requires sequences of length 100 or more:

In[12]:=
ResourceFunction["SerialRandomnessTest"][RandomInteger[{0, 1}, 50]]

Neat Examples

Visualize the sampling distribution of the test statistic:

In[13]:=
samp = Table[
   ResourceFunction["SerialRandomnessTest"][
    RandomInteger[{0, 1}, 1000], "TestStatistic"], 1000];
In[14]:=
Histogram[samp, Automatic, "PDF"]
Out[14]=
In[15]:=
estdist = FindDistribution[samp, TargetFunctions -> {GammaDistribution}]
Out[15]=
In[16]:=
Show[Histogram[samp, Automatic, "PDF"], Plot[PDF[estdist, x], {x, 0, 10}, PlotRange -> All]]
Out[16]=

Resource History

Source Metadata

See Also

Author Notes

This test is entirely derived form the NIST publication.

License Information