Skip to content

Commit

Permalink
Adds test to verify randomness for randint (#1657)
Browse files Browse the repository at this point in the history
Co-authored-by: joshmarshall1 <[email protected]>
  • Loading branch information
joshmarshall1 and joshmarshall1 authored Aug 8, 2022
1 parent c8ce6c4 commit 38b25fc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/pdarray_creation_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime as dt
import math
import statistics
from collections import deque

import numpy as np
Expand Down Expand Up @@ -811,3 +813,37 @@ def test_clobber(self):
npa += 1
self.assertTrue(np.all(a == i + 1))
self.assertTrue(np.all(npa == i + 1))

def test_randint_randomness(self):
minVal = 0
maxVal = 2**32
size = 250
passed = 0
trials = 20

for x in range(trials):
l = ak.randint(minVal, maxVal, size)
l_median = statistics.median(l.to_ndarray())

runs, n1, n2 = 0, 0, 0

# Checking for start of new run
for i in range(len(l)):
# no. of runs
if (l[i] >= l_median > l[i - 1]) or (l[i] < l_median <= l[i - 1]):
runs += 1

# no. of positive values
if (l[i]) >= l_median:
n1 += 1
# no. of negative values
else:
n2 += 1

runs_exp = ((2 * n1 * n2) / (n1 + n2)) + 1
stan_dev = math.sqrt((2 * n1 * n2 * (2 * n1 * n2 - n1 - n2)) / (((n1 + n2) ** 2) * (n1 + n2 - 1)))

if abs((runs - runs_exp) / stan_dev) < 1.9:
passed += 1

self.assertGreaterEqual(passed, trials * 0.8)

0 comments on commit 38b25fc

Please sign in to comment.