import matplotlib.pyplot as plt
import numpy as np
import time
from our_random import test
seed = .123
def rand():
"""Our very own random number generator."""
# The seed is always a number between 0 and 1.
global seed
# Get the time as a floating point number in seconds, multiply it by the seed
# (after adding 1.0 to the seed so we never multiple by 0), and get the 10
# digits after the decimal as a string in reverse order (so the quickest
# changing digit is first.
tmp = time.time() * (1.0 + seed)
tmp = tmp - int(tmp)
digits = ''.join(reversed("{:.10f}".format(tmp)[2:]))
# Prepend '0.' to the digits as our random number and save it as the seed for
# next time.
result = float('0.' + str(digits))
seed = result
return result
# Number of random numbers to generate for each test.
NUM_TEST_SAMPLES = 1000000
# Do a single test of our random number generator and plot a histogram.
numbers = np.array([rand() for _ in xrange(NUM_TEST_SAMPLES)])
NUM_BINS = 100
plt.hist(numbers, bins=NUM_BINS)
plt.axhline(y=(NUM_TEST_SAMPLES / float(NUM_BINS)), color='red')
print 'pvalue', test(numbers)
pvalue 0.0510114145228
# Define some functions to test our random number generator as well as
# numpy's generator. For each generator, we call the provided test
# function 'NUM_EXPERIMENTS' times with 'NUM_TEST_SAMPLES' random
# samples and then summarize the results.
NUM_EXPERIMENTS = 100
def test_our_random():
"""Get the p-value for a single test of our generator."""
return test([rand() for _ in xrange(NUM_TEST_SAMPLES)])
def test_numpy_random():
"""Get the p-value for a single test of numpy's generator."""
return test(np.random.random(NUM_TEST_SAMPLES))
def get_pvals(test_func):
"""Return an array of p-values from the given test function."""
pvals = np.array([test_func() for _ in xrange(NUM_EXPERIMENTS)])
return pvals
def summarize_results(name, pvals):
"""Print a summary of the results from an array of p-values."""
print "{} results:".format(name)
print " mean: {}".format(np.mean(pvals))
print " std: {}".format(np.std(pvals))
print " pvals: {}".format(" ".join("{:.2f}".format(pval) for pval in pvals))
# Summarize numpy's results.
numpy_pvals = get_pvals(test_numpy_random)
summarize_results('numpy', numpy_pvals)
numpy results: mean: 0.552962303605 std: 0.29984188423 pvals: 0.98 0.99 0.48 0.64 0.98 0.42 0.97 0.78 0.89 0.05 0.98 0.88 0.79 0.61 0.70 0.62 0.15 0.61 0.04 0.94 0.86 0.21 0.98 0.69 0.90 0.70 0.09 0.37 0.31 0.73 0.34 0.31 0.70 0.96 0.48 0.31 0.76 0.12 0.08 0.74 0.21 0.57 0.62 0.99 0.16 0.97 0.61 0.46 0.76 0.63 0.83 0.01 0.49 0.70 0.98 0.18 0.64 0.16 0.33 0.84 0.43 0.41 0.72 0.44 0.92 0.44 0.28 0.90 0.81 0.64 0.09 0.01 0.24 0.31 0.71 0.75 0.69 0.84 0.96 0.01 0.53 0.17 0.78 0.58 0.47 0.85 0.47 0.06 0.97 0.48 0.79 0.30 0.18 0.05 0.17 0.81 0.40 0.71 0.44 0.21
# Summarize our results. Takes quite a few minutes to run.
our_pvals = get_pvals(test_our_random)
summarize_results('our', our_pvals)
our results: mean: 0.503211314223 std: 0.284857700816 pvals: 0.65 0.85 0.08 0.83 0.41 0.85 0.83 0.57 0.04 0.85 1.00 0.28 0.04 0.73 0.07 0.78 0.47 0.19 0.44 0.71 0.04 0.05 0.08 0.85 0.58 0.09 0.53 0.27 0.83 0.48 0.37 0.45 0.37 0.13 0.46 0.08 0.74 0.86 0.21 0.99 0.54 0.25 0.31 0.78 0.53 0.59 0.67 0.38 0.19 0.05 0.85 0.54 0.84 0.52 0.22 0.53 0.98 0.95 0.74 0.86 0.56 0.17 0.20 0.00 0.26 0.65 0.23 0.75 0.27 0.74 0.88 0.82 0.62 0.62 0.54 0.94 0.38 0.44 0.29 0.13 0.45 0.65 0.68 0.19 0.32 0.71 0.32 0.23 0.92 0.26 0.26 0.93 0.65 0.43 0.57 0.42 0.93 0.05 0.65 0.77