I need to define a function which generates a random real number between 0 and $2\pi$, as a random phase constant for a wave superposition. The output should be different each time I re-run the code, with possibly an optional "fixing" parameter (so the random numbers are kept the same at each re-run of the code). The wave is defined as a linear superposition of functions like this one, where $n$ is an integer :
Phi[t_, x_, n_] := Sin[n Pi x] Sin[n Pi t + PhaseCste[n]]
The function "PhaseCste[n]" is the random phase number which I need. It should output a different random value for each $n = 1, 2, 3, \ldots, \infty$. Using the following doesn't work :
PhaseCste[n] = RandomReal[{0, 2Pi}]
because the function "Phi[t_, x_, n_]" should be called with the same values of the "PhaseCste" in the rest of the code (I'm using the same function "Phi[t, x, n]" several times elsewhere in the code).
I hope the question is clear. Any idea how to achieve this ?
Please, take note that the code should be compatible with Mathematica 7.0.
PhaseCste[n_]:=PhaseCste[n]=2Pi RandomReal[]? – AccidentalFourierTransform Jan 12 '16 at 15:18PhaseCste[n_]:=PhaseCste[n]does: it remembers the output for later. If you use the samen, you get the same random number. Try this simplified code:f[n_]:=f[n]=RandomReal[]; {f/@Range[10] ,f/@Range[10]}– AccidentalFourierTransform Jan 12 '16 at 15:24Clear[PhaseCste]at the beginning of your code. – AccidentalFourierTransform Jan 12 '16 at 15:29list = {}; Table[(While[MemberQ[list, r = RandomReal[{2, 2 \[Pi]}]], list = Join[list, {r}];]; r), {i, 1, 1000}];– Basheer Algohi Jan 12 '16 at 15:55RandomRealinlistand then generate new one and if it is not in the accumulatedlistthen it uses it. It can be used like this:PhaseCste := (While[MemberQ[list, r = RandomReal[{0, 2 \[Pi]}]], list = Join[list, {r}];]; r)– Basheer Algohi Jan 12 '16 at 16:17