Skip to content

Instantly share code, notes, and snippets.

@dooglus
Created December 3, 2017 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dooglus/0fa81be1f9b087142ef987a06d6824d3 to your computer and use it in GitHub Desktop.
Save dooglus/0fa81be1f9b087142ef987a06d6824d3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import random, string, time
def rand():
return start_date + random.random() * (end_date - start_date)
def fmt(seconds):
return '[%s]' % time.ctime(seconds)
def find(seconds):
last = None
for sec in times:
if sec < seconds:
return sec, last
last = sec
datfile = "time-between-blocks2.dat"
count = 0
lines = 100000
samples = 100000
times = []
fp = open(datfile, "r")
while True:
line = fp.readline()
if not line:
break
line = string.split(line[:-1])
times.append(string.atoi(line[1]))
count += 1
if (count == lines):
break
start_date = times[-1]
end_date = times[0]
print "picking random dates between %s and %s" % (fmt(start_date), fmt(end_date))
before_sum = 0
after_sum = 0
count = 0
while True:
t = rand()
before, after = find(t)
before_sum += t - before
after_sum += after - t
count += 1
if count % 1000 == 0:
print ("(%6d) %s is %6.2f seconds after %s (%6.2f) and %6.2f seconds before %s (%6.2f)" %
(count,
fmt(t),
t - before, fmt(before), before_sum / count,
after - t, fmt(after), after_sum / count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment