2
$\begingroup$

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

Manipulate[
 ListLinePlot[
  {
   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?

$\endgroup$

3 Answers 3

4
$\begingroup$
DynamicModule[{randomNormal, randomCauchy, newRandomData}, 
 Manipulate[
  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[];)
 ]
$\endgroup$
0
2
$\begingroup$

Simple and elegant

Manipulate[
ListPlot[list, PlotRange -> {{0, n}, {0, 1}}],
{{n, 10}, 1, 20, 1},
{{list, RandomReal[1, 10]}, ControlType -> None},
Button["Generate", {ngen = n; list = RandomReal[1, ngen]}]
]
$\endgroup$
2
$\begingroup$
Manipulate[
 SeedRandom[seed]; 
 (...),
{n, 50, 1000}, {seed, 0, 100, 1}]
$\endgroup$

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.