Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Conduct a runs–based test on a sequence of zeros and ones
| 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. | 
| "TestStatistic" | returns the test statistic | 
| "PValue" | returns the p-value associated with the test | 
Generate a sequence of random integers and apply a runs-based test:
| In[1]:= | ![sequence = RandomInteger[{0, 1}, 1000];](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/02237871f8ceb97e.png) | 
Visualize the sequence:
| In[2]:= | ![ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/2f90ebc98f880518.png) | 
| Out[2]= |  | 
| In[3]:= | ![ResourceFunction["BinaryRunRandomnessTest"][sequence, "TestStatistic"]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/6bf8166da49c654c.png) | 
| Out[3]= |  | 
| In[4]:= | ![ResourceFunction["BinaryRunRandomnessTest"][sequence, "PValue"]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/12e01eb3cf292acb.png) | 
| Out[4]= |  | 
Reject a non-random sequence:
| In[5]:= | ![subsequence = RandomInteger[{0, 1}, 100];](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/3ed700cc4cf24c87.png) | 
| In[6]:= | ![sequence = Flatten@Table[subsequence, 10];](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/731b65f6838def8b.png) | 
| In[7]:= | ![ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/2ab1ab413ecc4a58.png) | 
| Out[7]= |  | 
| In[8]:= | ![ResourceFunction["BinaryRunRandomnessTest"][sequence, "TestStatistic"]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/7fe63e8324f32265.png) | 
| Out[8]= |  | 
| In[9]:= | ![ResourceFunction["BinaryRunRandomnessTest"][sequence, "PValue"]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/3150375da2e57840.png) | 
| Out[9]= |  | 
Test the randomness of rule 30:
| In[10]:= | ![rule30seq = CellularAutomaton[30, {{1}, 0}, {100000, 0}]\[Transpose][[1]];](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/26a4351b2c2a71ad.png) | 
| In[11]:= | ![ArrayPlot[Partition[Take[rule30seq, 1000], 16]\[Transpose], Sequence[
 ColorFunction -> "CandyColors", ImageSize -> Large]]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/7bcb881adba009e7.png) | 
| Out[11]= |  | 
| In[12]:= | ![ResourceFunction["BinaryRunRandomnessTest"][rule30seq]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/5a201363b2836de1.png) | 
| Out[12]= |  | 
BinaryRunRandomnessTest requires sequences of length 100 or more:
| In[13]:= | ![ResourceFunction["BinaryRunRandomnessTest"][RandomInteger[{0, 1}, 50]]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/360c0bf87ed779ca.png) | 
Visualize the sampling distribution of the test statistic:
| In[14]:= | ![samp = Table[
   ResourceFunction["BinaryRunRandomnessTest"][
    RandomInteger[{0, 1}, 1000], "TestStatistic"], 1000];](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/4c457b70aab7ec1a.png) | 
| In[15]:= | ![Histogram[samp, Automatic, "PDF"]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/21879129ff5a006b.png) | 
| Out[15]= |  | 
| In[16]:= | ![estdist = FindDistribution[samp, TargetFunctions -> {NormalDistribution}]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/3b754fc4ec3f366c.png) | 
| Out[16]= |  | 
| In[17]:= | ![Show[Histogram[samp, Automatic, "PDF"], Plot[PDF[estdist, x], {x, 0, 600}, PlotRange -> All]]](https://www.wolframcloud.com/obj/resourcesystem/images/377/37758a26-b874-460f-8be8-62dbb4d69f0d/55df0b4195897bf5.png) | 
| Out[17]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License