Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I want to plot two graphs (on the same plot). One which is the $\frac{\sum_{i=0}^n x_i}{n}$ and the other $\frac{\sum_{i=0}^n x_i}{n-1}$ where x is a set created by RandomInteger[{a,b},n] and then have n slowly increase. I was wondering if this would be a sort of nice convergence plot over a large interval. The problem is as seen I want all of them to use the one {n,c,d} table for their iteration but Sum wants a copy, Plot wants a copy and RandomInteger wants the length of it. How can I use one of it for all of those?

share|improve this question
They will converge to (a+b)/2 ... or am I missing something? – Dr. belisarius Apr 10 '12 at 1:53
p0[a_, b_, 1] := p0[a, b, 1] = RandomInteger[{a, b}]; p0[a_, b_, n_] := p0[a, b, n] = (p0[a, b, n - 1] (n - 1) + RandomInteger[{a, b}])/n; p1[a_, b_, n_] := p0[a, b, n] n/(n - 1); ListLinePlot[{Table[p0[0, 10, k], {k, 2, 50000}], Table[p1[0, 10, k], {k, 2, 50000}]}] – Dr. belisarius Apr 10 '12 at 1:54

Adding an additional (optional) parameter for increment, define

 datalistPlot[a_Integer, b_Integer, c_Integer, d_Integer, e_Integer: 1] := 
 Module[{left = Mean@RandomInteger[{a, b}, c]},{
 Table[{left=(n - 1) left/n + RandomInteger[{a, b}]/n, n left/(n - 1)}, {n, c, d, e}], 
 {c, d}, 
 (a + b)/2
 // ListLinePlot[#[[1]], 
 GridLines -> {None, {{#[[3]], Directive[Red, Thick]}}}, 
 DataRange -> #[[2]], PlotRange -> {Automatic, #[[3]] + {-1, 1}}] &

and use as

datalistPlot[list[0, 10, 10, 500, 10]

to get

enter image description here

share|improve this answer

Assuming you want each of the summations over two different randomly generated lists:

{Total@ RandomInteger[{a, b}, #]/#, 
     Total@ RandomInteger[{a, b}, #]/(# - 1)} & /@ Range[2, n] // 
  Transpose // ListLinePlot
share|improve this answer
Total[] might be a useful thing here. – J. M. May 4 '12 at 3:10
@J.M. Yes you are right... some pre version 5 habits showing up here :) – image_doctor May 4 '12 at 12:20
Don't worry; you aren't alone. I haven't fully shaken off old Mathematica habits of mine as well... – J. M. May 4 '12 at 12:23
Is there any advantage of Total over Plus@@? – celtschk May 4 '12 at 12:41
Some cursory experiments seem to suggest Total is probably faster. – image_doctor May 4 '12 at 12:59

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.