# RandomReal left randomly unevaluated

Can anyone explain this? In Mathematica 11.1, I have a piece of code that uses RandomReal. Every so often I will throw up a RandomReal::array error. This happens seemingly at random.

I have a variable called pop that contains 50 elements, each element is a set of 6 arrays of varying sizes. The following came from iteration 386, there was no problem in all previous iterations. This has happened as late as iteration 1,000 and as early as iteration 50 in previous runs.

"The array dimensions 3 given in position 2 of RandomReal[{-1,1},3] should be a list of non-negative machine-sized integers giving the dimensions for the result."

Below is the expression in question, pop[[10]] in this case;

{1.99835 (1 - Abs[RandomReal[{-1, 1}, 3]]) +
1.60555 Abs[
RandomReal[{-1, 1}, 3]], -1.86404 (1 -
Abs[RandomReal[{-1, 1}, 3]]) -
1.75705 Abs[
RandomReal[{-1, 1}, 3]], -0.358809 (1 -
Abs[RandomReal[{-1, 1}, 3]]) -
0.354962 Abs[RandomReal[{-1, 1}, 3]]}


This came from something of the form;

f[A_,B_]:=Module[{C=Abs[g[]]}, A C + (1-C) B]


Where g[] generates a random array of the appropriate size. This then causes problems in the rest of my code and stops everything. I'm not sure exactly what information is needed to solve this problem so I'll happily provide whatever is necessary.

EDIT: Extra definitions added as per a suggestion of Subho95.

Spec = {6, 6, 6, 3};
newMat[{IL_, L1_, L2_, OL_}] :=
{RandomReal[{-1, 1}, {L1, IL}],
RandomReal[{-1, 1}, L1],
RandomReal[{-1, 1}, {L2, L1}], RandomReal[{-1, 1}, L2],
RandomReal[{-1, 1}, {OL, L2}], RandomReal[{-1, 1}, OL]}

mutat[x_, MUTPR_, MUTSTR_] :=
If[RandomReal[] < MUTPR, x + RandomReal[{-MUTSTR, MUTSTR}], x]

breed[mat1_, mat2_, MUTPR_, MUTSTR_,Spec_] :=
Map[mutat[#, MUTPR, MUTSTR] &, mask mat1 + (1 - mask) mat2, {2}]
]


The problem occurs in breed.

EDIT2: Updated as per suggestion of user42582.

With SeedRandom[1] added at the beginning of everything the problem occurs at iteration 442. Regarding the function breed, it works as follows;
1st iteration, pop = a table of 50 newMat's
nth iteration, pop = getNewPop[pop,fit[pop],...]

getNewPop[pop_, fit_, BKEEP_, MUTPR_, MUTST_, Spec_] :=
Module[{best = Sort[fit, #1[[2]] > #2[[2]] &][[1 ;; BKEEP, 1]],
newpop, tempsamp},
newpop = Table[pop[[best[[i]]]], {i, 1, BKEEP}];
Do[
tempsamp = RandomSample[fit[[;; , 2]] -> pop, 2];
AppendTo[newpop,
breed[tempsamp[[1]], tempsamp[[2]], MUTPR, MUTST, Spec]];
, {i, 1, Length[pop] - BKEEP}];
newpop]


The function fit takes pop and returns a list of 50 pairs {n,fitness of pop[[n]]}, I haven't included this function here because it is quite complicated, it involves no randomness though.
The function getNewPop returns a new population, keeping the best BKEEP of the population and offspring for the rest.

EDIT3: I ran the program for a third time, the error didn't occur at iteration 442 this time, it came at iteration 525. All the other values of pop and fit have been the same as the previous run up to iteration 442.

EDIT4: Corrected a typo

EDIT5: I have updated breed as follows

breed[mat1_, mat2_, MUTPR_, MUTSTR_,Spec_] :=
Map[mutat[#, MUTPR, MUTSTR] &, mask mat1 + (1 - mask) mat2, {2}]
]


Where newMask is the same as newMat but with a range of {0,1} rather than {-1,1}. Using this everything appears to be working fine. But why?

• I think you should provide what your definition of g is and how f is being called in your progarm – Subho May 9 '18 at 10:11
• Question updated. – GenericUsrnme May 9 '18 at 10:35
• you are making massive use of RandomReal; please consider using BlockRandom or SeedRandom; providing the iteration step the error occurs is not helpful unless there is some sensible way to replicate the error; also please consider providing the inputs to breed – user42582 May 9 '18 at 13:01
• Okay, I've used SeedRandom[1] at the beginning. The problem occured at the 442th iteration. I ran it another time and it got past this iteration without error, it's currently still running. The question will be updated when/if the error comes. – GenericUsrnme May 9 '18 at 14:16
• Updated question. – GenericUsrnme May 9 '18 at 14:18

Inside the Module of getNewPop when declaring local variable best you sort the list fit-which contains pairs of encoded variable, fitness score-and then obtain the first BKEEP encoded variables, which you subsequently store in best. Now, best contains BKEEP-the best wrt their fitness score-encoded variables (or, alternatively what newMat outputs).

The first line of code, inside the Module is

newpop = Table[pop[[best[[i]]]], {i, 1, BKEEP}];


Now what this line was supposed to do was to store in newpop the BKEEP best individuals (an elitist step). In fact, what it actually does, is to use the i-th entry of best as a second argument passed to Part (remember that best contains ragged arrays-the encoded variables) where the first argument is pop (the population of 50 encoded variables).

Now, unless I'm missing something, this is a no-go on so many levels.

Also, going inside the Do function,

AppendTo[newpop, breed[tempsamp[[1]], tempsamp[[2]], MUTPR, MUTST, Spec]];


function breed is supplied with an extra argument namely Spec than what its definition prescribes (no Spec argument in breed[mat1_, mat2_, MUTPR_, MUTSTR_])

I am not saying that this is the ultimate cause of the error message you get but it is a strong indication that your code contains errors that somehow go unnoticed and eventually cumulatively produce unexpected behavior like the one you are documenting in your question.

My tentative suggestion would be to revisit your code and before even consider wrapping it in functions you should make sure every part of the algorithm works as expected.

• Best contains only integers between 1 and 50, I'm not why this should be a problem let alone wrong on so many levels? The absence of Spec in breed was a typo in my original post, it has been corrected. The problem is that the code works fine if it converges before it hits that error and it works fine after if I manually evaluate the expression. All the intermediate populations are sensible, not listss of nested functions/etc. Everything does work as expected until I get that error. – GenericUsrnme May 9 '18 at 19:37
• it would be a problem if it contained ragged arrays like the ones produced by newMat; if it contains integers then it doesn't have a problem; if you are certain that the code executes correctly then with what I have in my hands I cannot reproduce the error – user42582 May 9 '18 at 19:37