Re: For Loop and Array related

*To*: mathgroup at smc.vnet.net*Subject*: [mg58003] Re: [mg57971] For Loop and Array related*From*: Sseziwa Mukasa <mukasa at jeol.com>*Date*: Thu, 16 Jun 2005 05:36:03 -0400 (EDT)*References*: <200506150958.FAA29716@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On Jun 15, 2005, at 5:58 AM, mchangun at gmail.com wrote: > Hi All, > > I have an array with 100 elements, all set to zero initially. Then I > want to randomly choose one element and increase its value by one, and > repeat this 16000 times. Here is my code: > > Lattice = Table[0, {n, 100}]; > For[i = 1, i = 16000, i++, Lattice[[Random[Integer, {1, 100}]]]++] > > So now if I add all the elements in the list Lattice together, I > should > get 16000 (I use Total[Lattice] to get the sum of the list). But this > doesn't happen, and strangely, each time I run this, the sum of the > list is different! What am I doing wrong? > Using ++ (Increment) with the expression lattice[[Random[Integer, {1,100}]]] is part of your problem. Increment appears to evaluate its argument in a very odd way, you can see this if you use trace. Random effectively gets called twice and the assigned value is indeterminate. For example: In[146]:= a=Table[0,{3}]; Trace[a[[Random[Integer,{1,3}]]]++] a Out[147]= {a[[Random[Integer,{1,3}]]]]++,{{a,{0,0, 0}},{Random[Integer,{1,3}], 3},{0,0,0}[[3]], 0},{a[[Random[Integer,{1,3}]]]=1, 1},0} Out[148]= {0,1,0} You can see that Random was evaluated once resulting in a value of 3, then the third element of the list was extracted, resulting in a 0, then that value was incremented resulting in a 1, and now the important part: Random is called again (for some reason Trace doesn't show that but the expression a[[Random[Integer,{1,3}]]]=1 necessarily means that Random is evaluated again, in this case resulting in a value of 2 (we can infer that from the result), then the result of the incremented value from the first call to Random is assigned to the element at the second call of Random. I wish I could explain the logic more plainly, but I can't imagine how, I hope you can follow. Anyway the moral of this lesson is not to call Random in expressions that use Increment because the result is probably not what you intended. > Also I'm aware that a lot of Mathematica newbies try and write code > like it were C++ and I think i've fallen into this trap as well. > So is > there a different (more Mathematica) way which I can implement the > above? Basically your problem is you're fighting with the Mathematica evaluator, and you're right; your expectations based on languages like C are incorrect in this case. The structure you are searching for is called a composition, and you can generate those using the Combinatorica function RandomComposition eg. <<DiscreteMath`Combinatorica` lattice=RandomComposition[16000,100] (By the way when programming in Mathemetica it's best to use lower case characters to define variables to avoid potential name conflicts). If you are curious how RandomCompositions works you can use ??RandomComposition Regards, Ssezi

**References**:**For Loop and Array related***From:*"mchangun@gmail.com" <mchangun@gmail.com>