Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code:

ϕ[0] := π/4
ϕ[n_] := Exp[-n] ϕ[n - 1] + Sqrt[1 - Exp[-2 n]] M
M = RandomVariate[NormalDistribution[0, 1]]

For this recursion relation, is the normal deviate taking a random variable during each recursion or is it just taking one for the whole recursion process?


share|improve this question
M is a nailed thing in your code ... – Dr. belisarius Jul 20 '12 at 19:46
As it is one for the whole recursion. You can use M:=... if you need a new one at each recursion. – b.gatessucks Jul 20 '12 at 19:46
Thanks guys! Always helpful, I'll push you both up. – kηives Jul 20 '12 at 19:51
up vote 7 down vote accepted

Set (=) stores the value at the time of computation, while SetDelayed (:=) stores the definition, so that the value is computed again each time it is called. Therefore, writing

M = RandomVariate[NormalDistribution[0, 1]]

means using the same value over and over, while writing

M := RandomVariate[NormalDistribution[0, 1]]

means computing a new RandomVariate at each iteration.

Moreover, no matter what your choice is about M, while we're at it, you might want to do memoization for the whole process, i.e. write

ϕ[0] = π/4
ϕ[n_] := ϕ[n] = Exp[-n] ϕ[n - 1] + Sqrt[1 - Exp[-2 n]] M

This way when computing ϕ[n] you store the value in your memory, so that when computing ϕ[n + 1] you only have to recall it, instead of computing it from scratch. It is usually worth it to trade memory for speed this way.

share|improve this answer
Thank you. Here is some text. – kηives Jul 20 '12 at 21:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.