NB. tacit
run=: upd rec
upd=: [ ((({~ 0{::]) + 1{::])`(0{::])`[} - 2{::]) ~.@] ; ] (+//. ; ]) 1 <: [

NB. explicit
RUN=: UPD rec
UPD=: 4 : 0
 b -~ x u}~ (x {~ u=. ~.y) + y +//. b=. x >: 1
)
   
rec=: ((] + ] >: i.@[) (?@$ <:))@#


Slower on short vectors, faster on larger ones (j805):

   1e2 ts 'tick^:1e4#~20'
0.0408952 3840
   1e2 ts 'run^:1e4#~20'
0.0802917 4864
   1e2 ts 'RUN^:1e4#~20'
0.0636084 7040
   
   10 ts 'tick^:1e4#~100'
0.243663 23552
   10 ts 'run^:1e4#~100'
0.12502 11904
   10 ts 'RUN^:1e4#~100'
0.104644 14848

Probably because of =/.

Louis

> On 19 Jul 2017, at 18:37, Rob Hodgkinson <rhodg...@me.com> wrote:
> 
> Thanks Mike it formats and reads perfectly… nice job, Rob
> 
>> On 20 Jul 2017, at 8:02 am, 'Mike Day' via Programming 
>> <programm...@jsoftware.com> wrote:
>> 
>> This took me much longer than a lunch-break,  partly because we
>> were away in France,  and I was experimenting with J701 on my
>> iPad when Liz would let me.
>> 
>> I came up with a number of variants, of which the following is best.
>> It's slower and uses more space than Raul's for small problem-sizes,
>> but is perhaps better for larger problems,  eg n > 100 or so.
>> 
>> NB. a Boolean verb for whether a player donates,  which,
>> NB. of course, depends on whether he has any money:
>> 
>>  donation =: 1 & <.
>> 
>> NB. This verb lists the indexes of the random donee (? player donated to)
>> NB. for each player,  whether of not that player CAN donate.
>> NB. It assumes that its single argument is i.n where n is the number
>> NB. of players,  which is why its name ends in i (!):
>> 
>>  idoneei=:(#|(+?&.<:@#~@#))
>> 
>> NB.  The idea is to concatenate
>> NB.   (the old state minus donation) and (donation), namely
>> NB.   ((-,  ])donation)
>> NB.  and sum them as ordered with this left-hand key:
>> NB.   (i.n) concatenated with (idonee),  namely
>> NB.   (, idoneei)@(i.@#)
>> 
>> NB.  Here it is,  with a fix, f.
>> 
>> new =: ((,idoneei)@(i.@#) +//. ((-,  ])donation )) f.
>> 
>> NB. time & space:
>>  ts =: 6!:2 , 7!:2@]
>> 
>> NB. where my version is ok:
>>  ts'orig ^:5000 #~200'   NB. Xiao-Yong Jin's original suggestion
>> 5.72563 22912
>>  ts'tick ^:5000 #~200'   NB. Raul's revised version
>> 1.57339 77824
>>  ts'new ^:5000 #~200'    NB. The best I could manage
>> 0.922963 20992
>> 
>> NB. and where it isn't so good:
>>  ts'orig ^:5000 #~10'  NB. Xiao-Yong Jin
>> 0.341718 6272
>>  ts'tick ^:5000 #~10'  NB. Raul M
>> 0.0445439 3840
>>  ts'new  ^:5000 #~10'  NB. Mike D
>> 0.0676198 4864
>> 
>> Sorry for this late posting,  but it wasn't convenient to
>> write up and send until home and with laptop to hand.
>> 
>> Thanks,
>> Mike
>> 
>> NB. prepared in notepad, copied to Thunderbird, and trying
>> to force fixed width - hope it displays reasonably.
>> 
>> 
>> 
>> On 11/07/2017 19:33, Devon McCormick wrote:
>>> Reading the page referenced, I see where I was confused.  The problem
>>> starts out as 100 people each with $100, but then solves a variant of it
>>> for 45 people with $45 each, possibly so the animation is legible.  It's
>>> not clear why they even mention the first set of numbers.
>>> 
>>> On Mon, Jul 10, 2017 at 8:37 PM, Raul Miller <rauldmil...@gmail.com> wrote:
>>> 
>>>> Oops, I did not think about that "reflexive giving" wart.
>>>> 
>>>> Here's a fix for my version:
>>>> 
>>>>   tick=: (0 >. <:) + i.@# +/@:(=/)~ I.@:* (] + <:) +/@:* ?@# <:@#
>>>>   require 'stats'
>>>>   stddev"1 tick^:(i.10) 45#45
>>>> 0 0.977008 1.18705 1.73205 2.01133 2.15322 2.46798 2.86832 2.97719 3.1334
>>>>   tick^:10000]20#20
>>>> 3 20 1 15 10 38 6 8 23 24 11 90 33 67 5 13 7 20 1 5
>>>> 
>>>> Thanks,
>>>> 
>>>> --
>>>> Raul
>>>> 
>>>> On Mon, Jul 10, 2017 at 7:50 PM, 'Pascal Jasmin' via Programming
>>>> <programm...@jsoftware.com> wrote:
>>>>> 
>>>>> The number of $ available each round is the number of people with >&0.
>>>> The number of potential recipients is the population.  This simplification
>>>> though means its possible for someone to pay himself.
>>>>> 
>>>>> A correction to your version,
>>>>> 
>>>>> /:~@(-&1`]@.(=&0)"0 (#/.~@] + ~.@]{[)`(~.@])`[} >&0 # (?@#~@#@]`]`[} ]
>>>> I.@:= i.@#)@?@#~@#)^:21000 #~10
>>>>> That assumption would increase the likelihood of eventual
>>>> "superconcntration"  but that doesn't seem to happen.
>>>>> As soon as some have 0, the rest have a "negative return" expectation
>>>> each turn.
>>>>> 
>>>>> ________________________________
>>>>> 
>>>>> 
>>>>> From: Xiao-Yong Jin <jinxiaoy...@gmail.com>
>>>>> 
>>>>> To: "programm...@jsoftware.com" <programm...@jsoftware.com>
>>>>> 
>>>>> Sent: Monday, July 10, 2017 3:43 PM
>>>>> 
>>>>> Subject: [Jprogramming] Everyone giving dollars to random others
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> I thought this is a good lunch break exercise.
>>>>> 
>>>>> 
>>>>> 
>>>>> http://www.decisionsciencenews.com/2017/06/19/counterintuitive-
>>>> problem-everyone-room-keeps-giving-dollars-random-others-
>>>> youll-never-guess-happens-next/
>>>>> 
>>>>> 
>>>>> Quote: “Imagine a room full of 100 people with 100 dollars each. With
>>>> every tick of the clock, every person with money gives a dollar to one
>>>> randomly chosen other person. After some time progresses, how will the
>>>> money be distributed?”
>>>>> 
>>>>> 
>>>>> And I came up with this simulation.  (45 people with 45 dollars each,
>>>> same as on the webpage.)
>>>>> 
>>>>> 
>>>>> /:~@(-&1`]@.(=&0)"0 (#/.~@] + ~.@]{[)`(~.@])`[} >&0 # (?@#~@#@]`]`[} ]
>>>> I.@:= i.@#)^:_@?@#~@#)^:5000 #~45
>>>>> 
>>>>> 
>>>>> And I really like to see the mean expectations and the standard
>>>> deviation of the mean, I ended up doing this.  (Make sure the J sentence is
>>>> one long line if you want to try it out.)
>>>>> 
>>>>> 
>>>>>    ((],.[:(+/%#*<:@#)&.:*: -"1)+/%#)}.(/:~@(-&1`]@.(=&0)"0 (#/.~@] +
>>>> ~.@]{[)`(~.@])`[} >&0 # (?@#~@#@]`]`[} 
>>>> ]I.@:=i.@#)^:_@?@#~@#)^:10000)^:(<65)
>>>> #~45
>>>>> 
>>>>> 0.984375 0.124984
>>>>> 
>>>>> 
>>>>> 2.01562 0.170335
>>>>> 
>>>>> 
>>>>>  3.0625 0.180765
>>>>> 
>>>>> 
>>>>> 4.15625 0.230616
>>>>> 
>>>>> 
>>>>> 5.21875 0.248476
>>>>> 
>>>>> 
>>>>>  6.1875 0.306894
>>>>> 
>>>>> 
>>>>> 7.35938 0.332862
>>>>> 
>>>>> 
>>>>> 8.64062 0.362809
>>>>> 
>>>>> 
>>>>> 9.84375 0.380723
>>>>> 
>>>>> 
>>>>> 10.9688 0.383805
>>>>> 
>>>>> 
>>>>>  12.125 0.407944
>>>>> 
>>>>> 
>>>>> 13.6406 0.423964
>>>>> 
>>>>> 
>>>>> 15.0625 0.457453
>>>>> 
>>>>> 
>>>>> 16.5312 0.462893
>>>>> 
>>>>> 
>>>>> 18.1875 0.480717
>>>>> 
>>>>> 
>>>>> 19.7031 0.464877
>>>>> 
>>>>> 
>>>>> 21.1562 0.475709
>>>>> 
>>>>> 
>>>>> 22.7031 0.483702
>>>>> 
>>>>> 
>>>>> 24.4062 0.516935
>>>>> 
>>>>> 
>>>>> 26.4062   0.5768
>>>>> 
>>>>> 
>>>>> 28.1094 0.604266
>>>>> 
>>>>> 
>>>>> 29.8281 0.617838
>>>>> 
>>>>> 
>>>>>  31.875  0.61942
>>>>> 
>>>>> 
>>>>> 33.9375 0.603345
>>>>> 
>>>>> 
>>>>> 35.7812 0.642211
>>>>> 
>>>>> 
>>>>> 37.9844 0.647987
>>>>> 
>>>>> 
>>>>> 40.6562 0.731321
>>>>> 
>>>>> 
>>>>>      43 0.739342
>>>>> 
>>>>> 
>>>>> 45.3906 0.723616
>>>>> 
>>>>> 
>>>>> 48.0469   0.7613
>>>>> 
>>>>> 
>>>>> 51.6094 0.823639
>>>>> 
>>>>> 
>>>>> 54.7656 0.873934
>>>>> 
>>>>> 
>>>>> 57.1406 0.815544
>>>>> 
>>>>> 
>>>>>      60 0.866598
>>>>> 
>>>>> 
>>>>> 63.8594 0.898319
>>>>> 
>>>>> 
>>>>>   68.75 0.918018
>>>>> 
>>>>> 
>>>>> 73.5781  1.00736
>>>>> 
>>>>> 
>>>>> 78.3281  1.08128
>>>>> 
>>>>> 
>>>>> 84.9688  1.25534
>>>>> 
>>>>> 
>>>>> 93.4688  1.29731
>>>>> 
>>>>> 
>>>>> 104.578  1.73274
>>>>> 
>>>>> 
>>>>> 114.922  2.07574
>>>>> 
>>>>> 
>>>>> 131.938   2.7357
>>>>> 
>>>>> 
>>>>> 159.359  4.40899
>>>>> 
>>>>> 
>>>>> 204.766  7.24239
>>>>> 
>>>>> 
>>>>> 
>>>>> So, how do I make a fancy animation (like the one in the webpage I
>>>> linked in the beginning of this message) with J?
>>>>> 
>>>>> ----------------------------------------------------------------------
>>>>> 
>>>>> 
>>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>>> ----------------------------------------------------------------------
>>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>> ----------------------------------------------------------------------
>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>> 
>>> 
>>> 
>> 
>> 
>> ---
>> This email has been checked for viruses by Avast antivirus software.
>> https://www.avast.com/antivirus
>> 
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to