Wolfram Research

Function Repository Resource:

BinaryRunRandomnessTest

Source Notebook

Conducts a runs–based test on a sequence of zeros and ones

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

ResourceFunction["BinaryRunRandomnessTest"][sequence]

conducts a runs–based randomness test on a sequence of zeros and ones and returns an associated p-value.

ResourceFunction["BinaryRunRandomnessTest"][sequence,"properties"]

conducts a runs–based randomness test on a sequence of zeros and ones and returns an associated 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 counting the total number of runs of both zeros and ones.
The test works only for sequences of zeros and ones.
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["BinaryRunRandomnessTest"][sequence, "TestStatistic"]
Out[3]=
In[4]:=
ResourceFunction["BinaryRunRandomnessTest"][sequence, "PValue"]
Out[4]=

Applications

Reject a non-random sequence:

In[5]:=
subsequence = RandomInteger[{0, 1}, 100];
In[6]:=
sequence = Flatten@Table[subsequence, 10];
In[7]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[7]=
In[8]:=
ResourceFunction["BinaryRunRandomnessTest"][sequence, "TestStatistic"]
Out[8]=
In[9]:=
ResourceFunction["BinaryRunRandomnessTest"][sequence, "PValue"]
Out[9]=

Test the randomness of rule 30:

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

Possible Issues

BinaryRunRandomnessTest requires sequences of length 100 or more:

In[13]:=
ResourceFunction["BinaryRunRandomnessTest"][RandomInteger[{0, 1}, 50]]

Neat Examples

Visualize the sampling distribution of the test statistic:

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

Resource History

Source Metadata

License Information