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 = 
     ConstantArray[size, dim]]];
  amplitude = 
   Outer[Pkn[{##}] &, Sequence @@ ConstantArray[N@fftIndgen, dim]];

 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?


1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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