[Jprogramming] Everyone giving dollars to random others

Skip Cave skip at caveconsulting.com
Thu Jul 20 18:26:32 UTC 2017


My explicit, looping version:

NB. y is number of people and dollars

NB. x is iteration count

hist =: <: @ (#/.~) @ (i.@#@[ , I.) NB. Histogram


iter =: 4 :0

nx =: y$y NB. initialize money

while. x>0

do. sg =: +/nx>0             NB. How many still giving?

nx =: nx-nx~:0                NB. Those that have, give

nx =: nx+(i.y) hist ?sg$y   NB. distribute dollars

x =. x-1

end.

)

ts =: 6!:2 , 7!:2@]

1e2 ts '1e4 iter 20'

0.0262932 4224


1e2 ts '1e4 iter 200'

0.0915201 20352


10 ts '1e4 iter 100'

0.0528825 9344

Skip Cave
Cave Consulting LLC


More information about the Programming mailing list