Skip to content

Instantly share code, notes, and snippets.

@jamii
Last active December 23, 2015 02:29
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 jamii/6567205 to your computer and use it in GitHub Desktop.
Save jamii/6567205 to your computer and use it in GitHub Desktop.
import random
import numpy.random
total_gain_from_swapping = 0
number_of_rounds = 0
while True:
# small_envelope = random.uniform(0, 1000) # this is well-behaved - the running mean eventually converges to 0
small_envelope = 2 ** numpy.random.geometric(0.5, 1)[0] # P($2^i) = 1/(2^i) - this is not well-behaved - the running mean wanders around forever
large_envelope = 2 * small_envelope
my_envelope, other_envelope = random.sample([small_envelope, large_envelope], 2)
total_gain_from_swapping += other_envelope - my_envelope
number_of_rounds += 1
if number_of_rounds % 10000 == 0:
print 'running mean gain from swapping is', float(total_gain_from_swapping) / number_of_rounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment