2

I have created a smooth function from a 1D random field. I used an existing code (Efficiently generating n-D Gaussian random fields) to generate random numbers with the correct Gaussian statistics, with which I created an interpolation function periodic in space. Below you can find how I have done this.

GaussianRandomField[size : (_Integer?Positive) : 256, 
  dim : (_Integer?Positive) : 1, Pk_: Function[k, k^-2]] := 
 Module[{Pkn, fftIndgen, noise, amplitude, s2}, 
  Pkn = Compile[{{vec, _Real, 1}}, 
    With[{nrm = Norm[vec]}, If[nrm == 0, 0, Sqrt[Pk[nrm]]]], 
    CompilationOptions -> {"InlineExternalDefinitions" -> True}];
  s2 = Quotient[size, 2];
  fftIndgen = ArrayPad[Range[0, s2], {0, s2 - 1}, "ReflectedNegation"];
  noise = 
   Fourier[RandomVariate[NormalDistribution[], 
     ConstantArray[size, dim]]];
  amplitude = 
   Outer[Pkn[{##}] &, Sequence @@ ConstantArray[N@fftIndgen, dim]];
  InverseFourier[noise*amplitude]]

 field = Abs@Delete[GaussianRandomField[], -1];
 u = GaussianFilter[field~Join~field~Join~field, 1];
 xrangeTriple = Range[1, Length[u]];
 uu = Interpolation[Transpose[{xrangeTriple, u}]]

 delta[x_] := uu[x + Length[field]]
 xrange = Range[0, Length[field]];
 Plot[delta[x], {x, xrange[[1]], xrange[[-1]]}]

Each time the notebook is ran, a different smooth function is plotted. My question is how do I adjust the code to plot the exact same smooth field each time I run the notebook?

SFTR
  • 23
  • 6

1 Answers1

0

Plop at SeedRandom[1] at the beginning of your code above, and it will repeat each run. The SeedRandom[ ] resets Mathematica's random number generator when called. You can change the seed as desired.

MikeY
  • 7,153
  • 18
  • 27