3
$\begingroup$

Rudimentary pseudoconceptial question:

I have a specific block of code that uses random variables and generates a plot at the end based on random code. I want my code to run 10 different times to get 10 different results. Essentially, I would like to execute this code like 10 times and generate 10 different plots. How would I modify the syntax using this?

Edit: It's giving me the same plot 10 times, which I don't want, am I doing something wrong?

It's giving me the same plot 10 times, which I don't want

$\endgroup$
  • 2
    $\begingroup$ Consider Table[] or Do[]. $\endgroup$ – J. M. is in limbo May 9 '15 at 4:29
  • 2
    $\begingroup$ Or (YourCode) & /@ Range[10];:D $\endgroup$ – Dr. belisarius May 9 '15 at 4:35
  • 1
    $\begingroup$ John, could you add just the code you used to your question, as TEXT, instead of the screenshot? I can't read well enough off of your screenshot. $\endgroup$ – MarcoB May 9 '15 at 4:45
  • 1
    $\begingroup$ @MarcoB i.stack.imgur.com/goXLq.png :P $\endgroup$ – Dr. belisarius May 9 '15 at 5:04
  • 1
    $\begingroup$ @belisarius Thank you, but I'll admit that I was hoping OP would save me the retyping :-) $\endgroup$ – MarcoB May 9 '15 at 5:22
4
$\begingroup$

I post this answer just as exploiting some other functions. Barring transcriptional, interpretational and indexing errors:

func[s0_, m_, s_, t_, h_] := Module[{d = Sqrt[h], n = t/h, rv, x},
  rv = {0}~Join~RandomVariate[NormalDistribution[0, d], n];
  x = Accumulate[rv];
  MapIndexed[{(#2[[1]] - 1) h, 
     s0 Exp[(m - s^2/2) (#2[[1]] - 1) h + s #1]} &, x]
  ]
sim[s0_, m_, s_, t_, h_, num_] := 
 Table[ListPlot[func[s0, m, s, t, h], Joined -> True], {num}]

Simulating nine walks (just to make grid):

Grid[Partition[sim[33.78, -0.124, 0.2725, 0.5, 0.001, 9], 3], 
 Frame -> All]

enter image description here

$\endgroup$
4
$\begingroup$

You can do this in very many ways.

Probably one of the simplest conceptually could be to wrap your repeating code in a Table with a constant iterator:

Table[
 Module[
  {data},
  data = RandomVariate[NormalDistribution[RandomReal[], RandomReal[]],
     100];
  Histogram[data]
  ],
 {6}
]

Many random plots

Alternatively, you could use control structures instructions such as While, For, Do... You can find more information on the guide on Loops and Control Structures in the documentation.

It may be worthwhile to demonstrate the usage of Do. Do does NOT return any output from inside its body. Therefore you need a way of collecting the output as it is generated (take a look at this tutorial on Collecting Expressions during Evaluation. Sow and Reap are a very efficient way to do that.

For instance, you could reproduce what we did above as follows:

Reap[
  Do[
   Module[
    {data},
    data = RandomVariate[NormalDistribution[RandomReal[], RandomReal[]], 100];
    Sow[ Histogram[data] ]
   ],
   {6}
  ]
] [[2]]

The result is a list of lists of the expressions generated by the repeated evaluation.

$\endgroup$

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