Last active
November 21, 2021 09:28
-
-
Save equal-l2/3802bcaee02f61939ae55887fb79ca7d to your computer and use it in GitHub Desktop.
Money distribution simulation
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
struct MoneySim | |
balance::Array{Int64} | |
members::Int64 | |
end | |
function init(;members::Int64=1000, initial_balance::Int64=100)::MoneySim | |
return MoneySim(fill(initial_balance, members), members) | |
end | |
function step!(self::MoneySim) | |
pass_to = fill(-1, self.members) | |
all_member = 1:self.members | |
for i in all_member | |
if self.balance[i] > 0 | |
pass_to[i] = rand(all_member) | |
end | |
end | |
for i in all_member | |
if pass_to[i] >= 0 | |
self.balance[i] -= 1 | |
self.balance[pass_to[i]] += 1 | |
end | |
end | |
end | |
using Plots | |
function draw(self::MoneySim, i::Int64; bins=:auto) | |
histogram(self.balance, bins=bins, normalize=true, xlims=(0, 400), ylims=(0,0.05), annotations=(300, 0.04, "i = $i")) | |
end | |
sim = init(members=1000) | |
@gif for i in 1:5000 | |
step!(sim) | |
draw(sim, i, bins=range(0, 400, length=20)) | |
end every 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired from https://twitter.com/masui/status/1436514323052969987
Result