def test_newrand_bounded_rand_int(range_, n_pts): """Test that `bounded_rand_int` follows a uniform distribution""" n_iter = 100 ks_pvals = [] uniform_dist = stats.uniform(loc=0, scale=range_) # perform multiple samplings to make chance of outlier sampling negligible for _ in range(n_iter): # Deterministic random sampling sample = [bounded_rand_int_wrap(range_) for _ in range(n_pts)] res = stats.kstest(sample, uniform_dist.cdf) ks_pvals.append(res.pvalue) # Null hypothesis = samples come from an uniform distribution. # Under the null hypothesis, p-values should be uniformly distributed # and not concentrated on low values # (this may seem counter-intuitive but is backed by multiple refs) # So we can do two checks: # (1) check uniformity of p-values uniform_p_vals_dist = stats.uniform(loc=0, scale=1) res_pvals = stats.kstest(ks_pvals, uniform_p_vals_dist.cdf) assert res_pvals.pvalue > 0.05, ( "Null hypothesis rejected: generated random numbers are not uniform." " Details: the (meta) p-value of the test of uniform distribution" f" of p-values is {res_pvals.pvalue} which is not > 0.05") # (2) (safety belt) check that 90% of p-values are above 0.05 min_10pct_pval = np.percentile(ks_pvals, q=10) # lower 10th quantile pvalue <= 0.05 means that the test rejects the # null hypothesis that the sample came from the uniform distribution assert min_10pct_pval > 0.05, ( "Null hypothesis rejected: generated random numbers are not uniform. " f"Details: lower 10th quantile p-value of {min_10pct_pval} not > 0.05." )
def test_newrand_set_seed(seed, val): """Test that `set_seed` produces deterministic results""" if seed is not None: set_seed_wrap(seed) x = bounded_rand_int_wrap(100) assert x == val, f'Expected {val} but got {x} instead'
def test_newrand_bounded_rand_int_limits(range_): """Test that `bounded_rand_int_wrap` is defined for unsigned 32bits ints""" with pytest.raises(OverflowError): bounded_rand_int_wrap(range_)