Re: For Loop and Array related
- To: mathgroup at smc.vnet.net
- Subject: [mg58022] Re: For Loop and Array related
- From: "Garrett" <garrett.mitchener at gmail.com>
- Date: Thu, 16 Jun 2005 05:37:02 -0400 (EDT)
- References: <d8oucl$t6q$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Try this: For[i = 1, i <= 100, i++, n = Random[Integer, {1, 10}]; Lattice[[n]]++; Print[{n, Total[Lattice], Lattice}]] Two things: It should be i <= 16000 (a comparison), not i = 16000 (an assignment) in your For statement. Also, after playing with this for a minute or two, I'm guessing that ++ is implemented internally as expr = expr + 1 But Random is a non-referentially transparent operation: Imagine expanding the ++ in your original form to ... Lattice[[ Random[...] ]] = Lattice[[ Random[...] ]] + 1 and you can see that it replaces one random element of the lattice by another random element plus one, hence the goofy behavior. So to get around this, you have to compute the random number and save it somewhere. Good luck, -- Garrett Mitchener