I have the following code:

    Brownian[x0_,μ_, σ_, t_, h_] := 
    Module[{d = Sqrt[h], m = t/h},
    g = Table[Random[NormalDistribution[0, d]], {m}];
    sums = FoldList[Plus, 0, g]; Table[X[i] = sums[[i + 1]], {i, 0, m}];
    geometric =  Table[x0*E^((μ - σ^2/2)*i*h + σ*X[i]), {i, 0, m}]]
    Brownian[78.55, 0.3693, 0.16689, .5, 0.001];
    Xt = geometric; m = 500; h = .001; 
    g1 = Table[{i*h, Xt[[i + 1]]}, {i, 0, m}];

This code works well, and it generates a g1 table as desired. However, I wish to loop this and generate many "g" tables (g1,g2,g3, etc.) all with different contents as there are random variables involved in my code. My desire is to be able to hit g1 and enter, and see contents in that table, then g2 and enter and see different contents. The reason why I want to do this is because I need to generate say 50 samples, and I would like to not want to hit enter every time.

I have tried a Do[ ...code...,{k,50}] and added a subscript k under "g" where there is a random function for the normal distribution, under sums, geometric, brownian, Xt, and g1 in hopes that I could hit like a g1(sub1) or a g1(sub2) and get different tables, yet I get errors and red bars. Anyone care to help me out?

  • $\begingroup$ Do you specifically want the data stored in many variables called g1,g2 ecc., or would be having it in a list of tables callable with g[[1]], g[[2]]... be enough? $\endgroup$
    – glS
    Commented May 26, 2015 at 6:31
  • $\begingroup$ @glance any name for the tables are fine as long as I can refer to them easily and the values are different. You can choose the names to your convenience. Thank you. $\endgroup$
    – John Yates
    Commented May 26, 2015 at 6:33
  • $\begingroup$ How are the gi defined? Identical to g1? If you want for example g2 = Table[{i*h, Xt[[i + 2]]}, {i, 0, m}] you would have a problem since Xt only has 501 elements $\endgroup$
    – glS
    Commented May 26, 2015 at 6:40
  • $\begingroup$ I want gi to still be defined as g1. g1 was just an arbitrary naming to distinguish itself from the g. Sorry for the confusion. It's not gn = Table[{ih, Xt[[i + n]]}, {i, 0, m}]; gi is always defined as: gi = Table[{ih, Xt[[i + 1]]}, {i, 0, m}]; I just want to pretty much run this code many times, like 50 times, and have different tables storing each result. I just called them g1, g2, etc. because I just wanted to refer to it as the 1st g table, 2nd g table, etc. $\endgroup$
    – John Yates
    Commented May 26, 2015 at 6:46
  • 1
    $\begingroup$ Any particular reason you're not using built-in capabilities (e.g. GeometricBrownianMotionProcess) to generate your random data? $\endgroup$
    – ciao
    Commented May 26, 2015 at 8:54

3 Answers 3


As noted in my comment, you can use much more efficient built-ins to accomplish this, e.g.

data = RandomFunction[GeometricBrownianMotionProcess[0.3693, 0.16689, 78.55],
                      {0, .5, 0.001}, 50];

ListPlot[data["Paths"], Joined -> True]

enter image description here

Should be orders of magnitude faster generating your random data...

  • $\begingroup$ aka rasher, glad to see your answers again...+1 :) $\endgroup$
    – ubpdqn
    Commented May 26, 2015 at 9:05

As glance as answered you could just create a table with multiplicity of desired runs.

I may have miscoded your intentions but I think you could also do as follows:

bm[x_, m_, s_, t_, h_] :=
 Module[{d = Sqrt@h, n = t/h, g, sm, gm},
  g = RandomVariate[NormalDistribution[0, d], n];
  sm = Accumulate[Prepend[g, 0]];
  gm = MapIndexed[ x Exp[(m - s^2/2) (#2[[1]] - 1) h + s #1] &, sm];
  MapIndexed[{(#2[[1]] - 1) h, #1} &, gm]
gtab = Table[bm[78.55, 0.3693, 0.16689, .5, 0.001], {50}];

Apart from the "stylistic" difference the output of bm is just what your g1 is (barring miscoding by me). If you still want access to the g you could just make the output of the function: {g, MapIndexed[{(#2[[1]] - 1) h, #1} &, gm] ]. Apologies for any misunderstanding or miscoding.

Here is example visualization based on gtab (ListPlot[#, PlotRange -> {0, 150}] & /@ gtab):

enter image description here


What about this?

Brownian[x0_, μ_, σ_, t_, h_] := 
 Module[{d = Sqrt[h], m = t/h,g,sums,X},
  g = Table[Random@NormalDistribution[0, d], {m}];
  sums = FoldList[Plus, 0, g];
  Do[X[i] = sums[[i + 1]], {i, 0, m}];
  Table[x0*E^((μ - σ^2/2)*i*h + σ*X[i]), {i, 0, m}]
m = 500; h = .001;
gList = Table[
   geometric = Brownian[78.55, 0.3693, 0.16689, .5, 0.001];
   Table[{i*h, geometric[[i + 1]]}, {i, 0, m}],

In this way the output of Brownian can be taken directly, without resorting to geometric the way you did. For each iteration of the outer Table the Brownian function is called generating a new set of data, and then the Table is generated the way you wanted.

To take the i-th generated set of data use


To take the value of the fifth element of the third generated list use


To check that the random data are generated differently each time use


As an aside, note that conventionally names starting with an uppercase letter are reserved to Mathematica builtins, so it would be better to call your function brownian.

  • $\begingroup$ Thank you VERY, VERY much! This works perfectly! I'll heed your advice on naming my function "brownian", as well. Your help is greatly appreciated. $\endgroup$
    – John Yates
    Commented May 26, 2015 at 7:15

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.