Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5478 > unrolled thread

Re: average of the consecutive coin tosses

Started byDarren Glosemeyer <darreng@wolfram.com>
First post2011-10-07 08:53 +0000
Last post2011-10-07 08:53 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: average of the consecutive coin tosses Darren Glosemeyer <darreng@wolfram.com> - 2011-10-07 08:53 +0000

#5478 — Re: average of the consecutive coin tosses

FromDarren Glosemeyer <darreng@wolfram.com>
Date2011-10-07 08:53 +0000
SubjectRe: average of the consecutive coin tosses
Message-ID<j6memk$7dp$1@smc.vnet.net>
On 10/6/2011 3:18 AM, michael partensky wrote:
> Hi.
>
> What are the better ways of doing what this guy does very inefficiently for
> the average of the consecutive coin tosses:
>
> tr = (flips = 1; (#/flips++)&  /@ Accumulate[Table[toss, {1000}]]) // N);
>
> where
>
> toss= RandomInteger[];
>
> Thanks
> Michael

When you want lots of random values, it is generally faster to get them 
all at once. In your example, we can also note that the denominators 
will just be a Range and take advantage of the listable nature of 
division, but we will want to make sure the values from Range are turned 
into floating point numbers to avoid exact arithmetic. Incorporating 
these ideas can make the computation much faster, as can be seen by 
timings for 1000 evaluations of each method:

In[2]:= toss = RandomInteger[];

In[3]:= Do[(flips = 1; (#/flips++) & /@ Accumulate[Table[toss, {1000}]] 
//N), {1000}]; // Timing

Out[3]= {2.683, Null}

In[4]:= Do[Accumulate[RandomInteger[1, 1000]]/N[Range[1000]], {1000}]; 
// Timing

Out[4]= {0.062, Null}


Darren Glosemeyer
Wolfram Research

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web