Skip to content

Instantly share code, notes, and snippets.

@skyzh
Last active July 28, 2017 14:39
Show Gist options
  • Save skyzh/e253a28dd09e1682e1415fe3d4be8c74 to your computer and use it in GitHub Desktop.
Save skyzh/e253a28dd09e1682e1415fe3d4be8c74 to your computer and use it in GitHub Desktop.
Mock Transaction: N people have `DEFALUT_WALLET` CNY at the beginning, and will transfer 1 CNY to another person in each round. After R rounds, this program will print how much money each person has.
#!/usr/bin/env python3
import random
DEFAULT_WALLET = 100
N = 100
R = 100
def do_transaction(storage, id_from, id_to):
storage[id_from] -= 1
storage[id_to] += 1
def check_transaction(storage, id_from):
return storage[id_from] > 0
def choose_target(id_from):
while True:
__target = random.randint(0, N - 1)
if __target != id_from:
return __target
def setup():
return [DEFAULT_WALLET] * N
def process(storage):
for __T in range(R):
for current in range(N):
if check_transaction(storage, current):
do_transaction(storage, current, choose_target(current))
def main():
for _ in range(100):
storage = setup()
process(storage)
print("Round %d" % (_ + 1), sorted(storage)[:15], sorted(storage)[-15:])
main()
#!/usr/bin/env python3
import numpy as np
N = 100
R = 1000
DEFAULT_WALLET = 100
__line = np.linspace(0, N - 2, N)
def process():
wealth = np.ones(N) * DEFAULT_WALLET
for _ in range(R):
seed = np.random.randint(0, N - 1, N)
seed += (seed >= __line)
seed[wealth <= _] = N
wealth += np.bincount(seed, minlength = N + 1)[:-1]
wealth -= np.ones(N) * R
return wealth
def main():
for _ in range(1000):
_result = sorted(process().tolist())
print("Round %d" % _, _result[:15], _result[-15:])
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment