Re: problem with RandomInteger
- To: mathgroup at smc.vnet.net
- Subject: [mg112196] Re: problem with RandomInteger
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 4 Sep 2010 04:00:56 -0400 (EDT)
On 9/3/10 at 6:10 AM, izrailsk at iacp.dvo.ru (Yuri Izrailsky) wrote:
>I have recently encountered a strange behavior of RandomInteger
>function when used inside Module construct. Here is an example:
>str1[] := Module[{l, i, j},
>l = {0, 0, 0};
>i = RandomInteger[{1, 3}];
>l[[i]] = 1;
>j = RandomInteger[{1, 3}];
>l[[j]]
>]
>In[3]:= N[Total[Table[str1[],{200}]]]/200 Out[3]= 0.345
>which is reasonably close to 1/3, but
>In[5]:= N[Total[Table[str1[],{1000}]]]/1000 Out[5]= 0.997
>looks very strange. What's happening here?
It looks like a bug. I can confirm I get essentially the same
result with your code using
In[21]:= $Version
Out[21]= 7.0 for Mac OS X x86 (64-bit) (February 19, 2009)
My testing indicates the point of failure occurs at n = 250.
That is,
N[Total[Table[str1[],{n}]]]/n returns a value of approximately 1
for n greater than 249
I would also note your code seems inefficient. Effectively you
are generating two random integers between 1 and 3 and giving
the result 1 when they are equal and 0 if they are not equal.
Then you construct a list of these values and compute the mean.
The same can be done with the following:
g[n_Integer] :=
Mean[Unitize[Subtract @@ RandomInteger[{1, 3}, {2, n}]] - 1]
But this code does not exhibit the problem.