Skip to content

Instantly share code, notes, and snippets.

@equal-l2
Last active November 21, 2021 09:28
Show Gist options
  • Save equal-l2/3802bcaee02f61939ae55887fb79ca7d to your computer and use it in GitHub Desktop.
Save equal-l2/3802bcaee02f61939ae55887fb79ca7d to your computer and use it in GitHub Desktop.
Money distribution simulation
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
@equal-l2
Copy link
Author

equal-l2 commented Nov 21, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment