Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I was playing with a Manipulateto visually compare calculating the mean of Normal and Cauchy random data.

   Table[Mean@Take[#, i], {i, 1000}] &[
    RandomVariate[NormalDistribution[10, 1], 1000]],
   Table[Mean@Take[#, i], {i, 1000}] &[
    RandomVariate[CauchyDistribution[10, 1], 1000]]
  PlotRange -> Full,
  DataRange -> n,
  Frame -> True,
  Axes -> False
 {n, 50, 1000}

The result looks nice, but there is one problem. It regenerates the random data each time I move the slider for n. Is there some way I could set it up so that is only generates new random data when the Manipulate is first evaluated and when a button is pushed?

share|improve this question

1 Answer 1

up vote 2 down vote accepted
DynamicModule[{randomNormal, randomCauchy, newRandomData}, 
  ListLinePlot[{Table[Mean@Take[#, i], {i, 1000}] &[randomNormal], 
    Table[Mean@Take[#, i], {i, 1000}] &[randomCauchy]}, 
   PlotRange -> Full, DataRange -> n, Frame -> True, 
   Axes -> False], {n, 50, 1000}, 
  Button["New random data", {randomNormal, randomCauchy} = newRandomData[]
 Initialization :> (
   newRandomData[] := 
    RandomVariate[#[10, 1], 1000] & /@ {NormalDistribution, CauchyDistribution};
   {randomNormal, randomCauchy} = newRandomData[];)
share|improve this answer

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.