Re: average of the consecutive coin tosses
- To: mathgroup at smc.vnet.net
- Subject: [mg122054] Re: average of the consecutive coin tosses
- From: Darren Glosemeyer <darreng at wolfram.com>
- Date: Tue, 11 Oct 2011 04:23:10 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201110060818.EAA22381@smc.vnet.net> <201110080935.FAA21116@smc.vnet.net>
On 10/8/2011 4:35 AM, DrMajorBob wrote:
> Darren,
>
> With that code, every toss would be the same.
>
> Bobby
>
> On Fri, 07 Oct 2011 03:45:57 -0500, Darren Glosemeyer
> <darreng at wolfram.com> wrote:
>
>> 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
>>
>
Yes, you are correct. The toss code should have either used toss:=... or
used RandomInteger[] directly in the Table. The faster code is correct
and would show an even greater speed improvement over the corrected toss
code.
Darren
- References:
- average of the consecutive coin tosses
- From: michael partensky <partensky@gmail.com>
- Re: average of the consecutive coin tosses
- From: DrMajorBob <btreat1@austin.rr.com>
- average of the consecutive coin tosses