Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

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


  • Prev by Date: Re: Help with the User Interface in Mathematica 5.1.1
  • Next by Date: Re: Re: spotlight escape sequences
  • Previous by thread: Re: For Loop and Array related
  • Next by thread: Re: For Loop and Array related