"""Reviewer-owned oracle and median audit; no bundle code is imported or run."""
import json
import random
import statistics
from fractions import Fraction
from pathlib import Path

n=1000000
conditions=[]
midpoint=1<<52
for t in range(50):
    rng=random.Random(2*(50*n+t)+1)
    # random() produces a multiple of 2^-53. Mixed inputs 2u-1
    # therefore have exact integer numerator int(u*2^53)-2^52 over 2^52.
    total=0
    magnitude=0
    for _ in range(n):
        z=int(rng.random()*(1<<53))-midpoint
        total+=z
        magnitude+=abs(z)
    conditions.append(magnitude/abs(total))
ordered=sorted(conditions)
result={'n':n,'samples':50,'median_convention':'arithmetic mean of the two middle order statistics',
        'lower_middle':ordered[24],'upper_middle':ordered[25],
        'usual_median':statistics.median(conditions),
        'rounded_usual_median':round(statistics.median(conditions),3),
        'rounded_upper_middle':round(ordered[25],3),
        'exact_integer_grid':'denominator 2^52; integer sums and magnitude sums; no math.fsum used',
        'scope':'Condition-number statistic only; other methods and full result table were not reproduced.'}
Path(__file__).with_name('independent_checks.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps(result,indent=2))
