Re: problem with RandomInteger
- To: mathgroup at smc.vnet.net
- Subject: [mg112217] Re: problem with RandomInteger
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 5 Sep 2010 05:27:17 -0400 (EDT)
- References: <i5qhhg$pkm$1@smc.vnet.net>
Am Sat, 4 Sep 2010 08:02:36 +0000 (UTC)
schrieb Albert Retey <awnl at gmx-topmail.de>:
...
> I think this is because Table does compile if it thinks that will
> improve speed. You can change the limit when this will start, e.g.:
>
> SetSystemOptions["CompileOptions" -> "TableCompileLength" -> 100000];
>
> now
>
> N[Total[Table[str1[], {10000}]]]/10000
>
> will return the expected result. I don't exactly understand why this
> goes wrong when compiling, but I think it is a bug...
>
> hth,
>
> albert
>
Indeed! Using a compiled version of str1[], there is no chance to get
reliable results:
In[1]:= strC = Compile[{}, Module[{l, i, j},
l = {0, 0, 0};
i = RandomInteger[{1, 3}];
l[[i]] = 1;
j = RandomInteger[{1, 3}];
l[[j]]], {{i | j, _Integer}, {l, _Integer, 1}}];
In[2]:= N[Total[Table[strC[], {10}]]]/10
Out[2]= 1.
In[3]:= N[Total[Table[strC[], {1000}]]]/1000
Out[3]= 1.
In[4]:= N[Total[ParallelTable[strC[], {1000}]]]/1000
Out[4]= 1.
But this gives the expected result:
In[1]:=
simp=Compile[{{n,_Integer}},Sum[Boole[RandomInteger[{1,3}]===2],{n}]/n//N];
In[2]:= simp/@(10^{1,2,3,4})
Out[2]= {0.3,0.36,0.34,0.3386}
Can anyone from Wolfram Inc. please explain this behaviour?
TIA,
Peter