Re: Re: Is this a bug?
- To: mathgroup at smc.vnet.net
- Subject: [mg32078] Re: [mg32061] Re: [mg32047] Is this a bug?
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Sat, 22 Dec 2001 04:22:52 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
I must confess I did not read the second half of the original message, assuming that it was basically the same thing as the first. Now that I have read it I would like just to observe that my "dynamic" programming solution to the first part can also be applied to the second (providing an alternative to Rob Pratt's approach): In[1]:= r[k_]:=r[k]=Random[Integer,{0,1}] In[2]:= tst[n_] := Module[{a}, a[0] = a[1] = 0; Do[a[r[k]]++,{k,n}]; {a[0],a[1]}] In[3]:= tst[50] Out[3]= {28,22} Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ On Friday, December 21, 2001, at 05:57 PM, Rob Pratt wrote: > On Thu, 20 Dec 2001, Paul van Wamelen wrote: > >> Is the following a bug? >> >> Mathematica 4.1 for Sun Solaris >> Copyright 1988-2000 Wolfram Research, Inc. >> -- Motif graphics initialized -- >> >> In[1]:= k[x_] := Module[{},Print["hallo world"];x]; >> >> In[2]:= a[5] = 0; >> >> In[3]:= a[k[5]]++ >> hallo world >> hallo world >> >> Out[3]= 0 >> >> >> I would have expected only one "hallo world" and it would seem to be >> more efficient to only evaluate the k[5] once... >> >> The above example is not important but in the form below it had me >> baffled for a while: >> >> In[4]:= tst[n_] := Module[{a}, >> a[0] = a[1] = 0; >> Do[a[Random[Integer,{0,1}]]++,{n}]; >> {a[0],a[1]}] >> >> In[5]:= tst[50] >> >> Out[5]= {24, 24} >> >> (Does not add up to 50!) >> >> Thanks! > > I encountered the second form just two days ago. I'll leave it to > somebody else to explain the above behavior, but I ended up using a > workaround like the following. > > tst[n_] := Module[{a, r}, > a[0] = a[1] = 0; > Do[(r = Random[Integer,{0,1}]; a[r]++), {n}]; > {a[0],a[1]}] > > Rob Pratt > Department of Operations Research > The University of North Carolina at Chapel Hill > > rpratt at email.unc.edu > > http://www.unc.edu/~rpratt/ > > > > > >