2

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?

Mr Alpha
  • 3,156
  • 2
  • 24
  • 33

3 Answers3

4
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[];)
 ]
Rojo
  • 42,601
  • 7
  • 96
  • 188
2

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]}]
]
Fabio
  • 1,357
  • 6
  • 13
2
Manipulate[
 SeedRandom[seed]; 
 (...),
{n, 50, 1000}, {seed, 0, 100, 1}]
A.G.
  • 4,362
  • 13
  • 18