Re: RandomReal gets stuck

• To: mathgroup at smc.vnet.net
• Subject: [mg100376] Re: RandomReal gets stuck
• From: Bas Straatman <bastraat at ucalgary.ca>
• Date: Tue, 2 Jun 2009 06:43:41 -0400 (EDT)
• Organization: The University of Calgary
• References: <h00d18\$pis\$1@smc.vnet.net>

```Hi,

I know the problem can't be found in the little piece of code that I
included in the first posting. For obvious reasons I didn't include all
code. But I don't know of any other way to reset the random number
generator than by making use of SeedRandom[] and nowhere in the notebook
or mathematica session I made use of that function. Therefore, I am
pretty sure that the problem is not in my code.

It does seem to be related to the use of Maximize. More in particular,
as Sjoerd de Vries noticed, it depends somehow on the number of
variables that Maximize has to deal with. Initially random reals in my
simulations appear to be random, but at some point, when the model grows
(I am simulating evolving artificial economies) the random number
generator starts to produce the same series of random numbers over and
over again.

It also doesn't matter whether or not Maximize is used non-sensical or
appropriately. Below I use variables y1, ...y32, not indexed by means of
square brackets, I even provide proper boundaries such that a solution
exist, and still the same issue with RandomReal turns up.

In[1]:= \$Version

Out[1]= "7.0 for Microsoft Windows (32-bit) (November 10, 2008)"

In[2]:= Table[RandomReal[], {5}]

Out[2]= {0.289496, 0.40001, 0.634792, 0.389652, 0.528876}

In[3]:= size = 30

Out[3]= 30

In[4]:= Maximize[
Plus @@ Table[ToExpression["y" <> ToString[i]], {i, size}],
Map[# <= 10 &, Table[ToExpression["y" <> ToString[i]], {i, size}]],
Table[ToExpression["y" <> ToString[i]], {i, size}]]

Out[4]= {300, {y1 -> 10, y2 -> 10, y3 -> 10, y4 -> 10, y5 -> 10,
y6 -> 10, y7 -> 10, y8 -> 10, y9 -> 10, y10 -> 10, y11 -> 10,
y12 -> 10, y13 -> 10, y14 -> 10, y15 -> 10, y16 -> 10, y17 -> 10,
y18 -> 10, y19 -> 10, y20 -> 10, y21 -> 10, y22 -> 10, y23 -> 10,
y24 -> 10, y25 -> 10, y26 -> 10, y27 -> 10, y28 -> 10, y29 -> 10,
y30 -> 10}}

In[5]:= Table[RandomReal[], {5}]

Out[5]= {0.911008, 0.0662658, 0.193939, 0.302682, 0.159269}

In[6]:= Maximize[
Plus @@ Table[ToExpression["y" <> ToString[i]], {i, size}],
Map[# <= 10 &, Table[ToExpression["y" <> ToString[i]], {i, size}]],
Table[ToExpression["y" <> ToString[i]], {i, size}]] ;
Table[RandomReal[], {5}]

Out[7]= {0.121323, 0.296845, 0.0190161, 0.531837, 0.65849}

In[8]:= size = 31

Out[8]= 31

In[9]:= Maximize[
Plus @@ Table[ToExpression["y" <> ToString[i]], {i, size}],
Map[# <= 10 &, Table[ToExpression["y" <> ToString[i]], {i, size}]],
Table[ToExpression["y" <> ToString[i]], {i, size}]]

Out[9]= {310, {y1 -> 10, y2 -> 10, y3 -> 10, y4 -> 10, y5 -> 10,
y6 -> 10, y7 -> 10, y8 -> 10, y9 -> 10, y10 -> 10, y11 -> 10,
y12 -> 10, y13 -> 10, y14 -> 10, y15 -> 10, y16 -> 10, y17 -> 10,
y18 -> 10, y19 -> 10, y20 -> 10, y21 -> 10, y22 -> 10, y23 -> 10,
y24 -> 10, y25 -> 10, y26 -> 10, y27 -> 10, y28 -> 10, y29 -> 10,
y30 -> 10, y31 -> 10}}

In[10]:= Table[RandomReal[], {5}]

Out[10]= {0.455719, 0.977826, 0.943215, 0.962216, 0.302348}

In[11]:= Maximize[
Plus @@ Table[ToExpression["y" <> ToString[i]], {i, size}],
Map[# <= 10 &, Table[ToExpression["y" <> ToString[i]], {i, size}]],
Table[ToExpression["y" <> ToString[i]], {i, size}]] ;
Table[RandomReal[], {5}]

Out[12]= {0.455719, 0.977826, 0.943215, 0.962216, 0.302348}

In[13]:= size = 32

Out[13]= 32

In[14]:= Maximize[
Plus @@ Table[ToExpression["y" <> ToString[i]], {i, size}],
Map[# <= 10 &, Table[ToExpression["y" <> ToString[i]], {i, size}]],
Table[ToExpression["y" <> ToString[i]], {i, size}]]

Out[14]= {320, {y1 -> 10, y2 -> 10, y3 -> 10, y4 -> 10, y5 -> 10,
y6 -> 10, y7 -> 10, y8 -> 10, y9 -> 10, y10 -> 10, y11 -> 10,
y12 -> 10, y13 -> 10, y14 -> 10, y15 -> 10, y16 -> 10, y17 -> 10,
y18 -> 10, y19 -> 10, y20 -> 10, y21 -> 10, y22 -> 10, y23 -> 10,
y24 -> 10, y25 -> 10, y26 -> 10, y27 -> 10, y28 -> 10, y29 -> 10,
y30 -> 10, y31 -> 10, y32 -> 10}}

In[15]:= Table[RandomReal[], {5}]

Out[15]= {0.455719, 0.977826, 0.943215, 0.962216, 0.302348}

In[16]:= Maximize[
Plus @@ Table[ToExpression["y" <> ToString[i]], {i, size}],
Map[# <= 10 &, Table[ToExpression["y" <> ToString[i]], {i, size}]],
Table[ToExpression["y" <> ToString[i]], {i, size}]] ;
Table[RandomReal[], {5}]

Out[17]= {0.455719, 0.977826, 0.943215, 0.962216, 0.302348}

I received an email from from Daniel Lichtblau (Wolfram Research) that
it probably is a bug, I have sent him the above example and hope that it
gets sorted out quickly.

Bas

Bill Rowe wrote:
> On 5/31/09 at 6:34 AM, sjoerd.c.devries at gmail.com (Sjoerd C. de
> Vries) wrote:
>
>> I can confirm this bug.
>
>> Executing an even simpler version, namely
>
>> Maximize[Plus @@ Table[y[i], {i, 31}], Table[y[i], {i, 31}]];
>
>> will also set the random generator to a fixed starting point. The
>> number 31 is crucial, as lower values do not appear to cause the
>> bug, while higher values do. Changing Plus to Times appears to
>> prevent the bug.
>
>> In[394]:= Table[
>> Maximize[Plus @@ Table[y[i], {i, 31}], Table[y[i], {i, 31}]];
>> RandomReal[], {20}
>> ]
>
> When I execute the code above, I do get the same result as you
> report. Clearly, there is a problem here somewhere. But it isn't
> clear the issue is with RandomReal.
>
> The code above uses Maximize in a non-sensical way, particularly
> when the function y hasn't been defined. This usage asks
> Mathematica to find the maximize the sum of n things with
> respect to each of the n things. It appears there is an attempt
> to use the notation y[n] to mean an array indexed by n rather
> than a function of n. So, Maximize correctly generates error
> messages. What is unexpected is poor input to Maximize appears
> to cause a problem for RandomReal.
>
> On my system, changing Plus to Times eliminates the effect on
> RandomReal even though there is still a non-sensical input to
> Maximize. Alternatively, defining y then executing the code does
>
> In[5]:= y[n_] := n^2 - n
>
> In[6]:= Table[
>   Maximize[Plus @@ Table[y[i], {i, 31}], Table[y[i], {i, 31}]];
>   RandomReal[], {20}]
>
> During evaluation of In[6]:= Maximize::ivar: 0 is not a valid
> variable. >>
>
> During evaluation of In[6]:= Maximize::ivar: 0 is not a valid
> variable. >>
>
> During evaluation of In[6]:= Maximize::ivar: 0 is not a valid
> variable. >>
>
> During evaluation of In[6]:= General::stop: Further output of
> Maximize::ivar will be suppressed during this calculation. >>
>
> Out[6]= {0.877054,0.833103,0.703786,0.0482118,0.226066,0.230746,0.0738072=
> ,0.680963,0.53264,0.989333,0.418793,0.951114,0.963168,0.870439,0.926361,0.267113,0.195084,0.810066,0.875896,0.579076}
>
>> This is very nasty, as a lot of people critically depend on random
>> functions to be random. I would urge you to report this to wolfram
>> support.
>
> I agree many people including myself depend on the random
> functions to be random. And I would also like Mathematica to
> fail gracefully when given non-sensical input and provide useful
> error messages. But I am never surprised when software does
> something other than fail gracefully given non-sensical input or
> provides less than useful error messages.
>
> Entering
>
> Table[
>   Maximize[Plus @@ Table[y[i], {i, 31}], Table[y[i], {i, 31}]];
>   RandomReal[],
>   {20}
>   ]
>
> into a new session and expecting useful output simply isn't a
> reasonable expectation.
>
> Note, this in no way says what the original poster was doing is
> unreasonable or non-sensical. The rest of the information needed
> to determine whether what the original poster was attempting is
> sensible hasn't been provided. I would also note, that sending a
> bug report to Wolfram without the additional information is
> probably pointless.
>
>

```

• Prev by Date: Re: problem with reduce
• Next by Date: Re: Re: problem writing debugging utility function
• Previous by thread: Re: RandomReal gets stuck
• Next by thread: Re: RandomReal gets stuck