Last active
November 3, 2015 02:51
-
-
Save riceluxs1t/b53978195378bf37b416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
SWIPE = 0.1 # Percentage of times user unlocks when he sees rewards. | |
# Sample randomly from the seconds space of a day, N times. | |
# Simulates user on/off actions | |
def sample_ticks(n): | |
wakeups = [random.randrange(1, 3600*24 + 1) for _ in range(n)] | |
wakeups.sort() | |
return wakeups | |
# Timer gets auto-reset at the start of every hour. | |
# Random wakeups get rewarded if it and its previous wakeup are in different hours. | |
def before_compute(wakeups): | |
s = 0 | |
for i in range(1, len(wakeups)): | |
if wakeups[i-1]/3600 != wakeups[i]/3600 and random.random() > SWIPE: | |
s += 1 | |
return s | |
# Newly implemented method. | |
# manually updates timers when user turns his screen on or off. | |
def after_compute(wakeups): | |
c = 3600 | |
s = 0 | |
offset = 1 | |
last_offset = 0 | |
for i in range(0, len(wakeups)): | |
if offset == 1: | |
if random.random() > SWIPE: | |
s += 1 | |
offset = 0 | |
if wakeups[i] - last_offset > c: | |
last_offset = wakeups[i] | |
else: | |
if wakeups[i] - last_offset > c: | |
last_offset = wakeups[i] | |
if random.random() > SWIPE: | |
s += 1 | |
else: | |
offset = 1 | |
return s | |
# vary the average number of screen-on actions and compare | |
def experiment(): | |
for i in range(5, 85, 5): | |
run(i, 10000) | |
# simulate based on n = avg # of screen-ons. p = # of repeats. | |
def run(n, p): | |
avg=0 | |
avg_aft=0 | |
for _ in range(p): | |
samples = sample_ticks(n) | |
avg += before_compute(samples) | |
avg_aft += after_compute(samples) | |
print "for {0} ...before = {1}".format(n,float(avg)/10000) | |
print "for {0} ...after = {1}".format(n,float(avg_aft)/10000) | |
print "comparinng....{0}".format(float(avg_aft)*100/float(avg)) | |
if __name__ == "__main__": | |
experiment() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment