# Adding random numbers to the arguments of a sum of cosines

This is probably really a simple question.

I would like to plot a sum of cosines with increased argument and for each cosine in sum I would like to add random value:

Plot[Sum[Cos[n*x + RandomReal[{0, 2 Pi}]], {n, 100}], {x, -10, 10}]


However it seems to me that the program, while making a plot, generates a new random number for each x-point plotted. Therefore, even for single cosine (Plot[Cos[x + RandomReal[{0, 2*Pi}]], {x, -10, 10}] ) I get a completely noisy graph of random cosine values.

I would be very grateful if somebody can tell me what to do to prevent the program from generating new random numbers at each x-point.

• Just compute the random offset once: With[{r = RandomReal[{0, 2 Pi}]}, Plot[Cos[x + r], {x, -10, 10}]] Mar 29, 2017 at 10:55
• Thank you for answer. I tried it before, but then the problem is that i have the same random number for each cosine in my sum. And i would like to generate a new one for each (but not for each x when plotting). Mar 29, 2017 at 11:08
• Generate as many as you need: With[{r = RandomReal[{0, 2 Pi}, 10]}, Plot[Total[Cos[Range x + r]] // Evaluate, {x, -10, 10}]] Mar 29, 2017 at 11:14

J.M. has answered the question in the comments. This is to get that answer (slightly edited) on record in a more visible form. For 10 terms, we get

SeedRandom;
With[{n = 10},
With[{r = RandomReal[{0, 2 Pi}, n]},
Plot[Total[Cos[Range[n] x + r]], {x, -10, 10}]]]


For a 100 terms, which is what the OP asked for, the plot must be stretched out to see the details better.

SeedRandom;
With[{n = 100},
With[{r = RandomReal[{0, 2 Pi}, n]},
Plot[Total[Cos[Range[n] x + r]], {x, -10, 10},
AspectRatio -> 1/5,
ImageSize -> Full]]] 