The following code creates an array of length N, such that the sum of elements is less than or equal to p and each element is between 0 and l. All elements are integers. (code courtesy, Kellen Myers Creating a fast random array generator)

    MakeRow[n_, l_, p_] := 
    Module[{v = RandomComposition[p, n]}, 
    While[Max[v] > l, v = RandomComposition[p, n]];

Now I make a Table of 1000 such arrays by two methods, Table and ParallelTable.Parallel table gives the wrong answer.

Flatten@Table[MakeRow[8, 4, 5], {i, 1000}] // Tally // Sort

Output{{0, 4618}, {1, 2156}, {2, 902}, {3, 256}, {4, 68}}

Flatten@ParallelTable[MakeRow[8, 4, 5], {i, 1000}] // Tally // Sort

Output {{0, 4680}, {1, 2095}, {2, 861}, {3, 280}, {4, 77}, {5, 7}}

From the output of parallel table , one can see that there are 7 5's which should not exist as the code specifically constraints the value of each element to 4. I have used Module and hence the variables are local. Cannot figure out this problem. Please help. Using Mathematica 9


1 Answer 1


Packages need to be loaded explicitly for parallel kernels using ParallelNeeds.

Just do ParallelNeeds["Combinatorica`"] before running the code.

  • $\begingroup$ Thanks it works, but I get this error the first time I run the code. ( I was getting this even before ParallelNeeds) General::compat: Combinatorica Graph and Permutations functionality has been superseded by preloaded functionality. The package now being loaded may conflict with this. Please see the Compatibility Guide for details. $\endgroup$
    – akhileshsk
    Feb 6, 2015 at 1:15
  • $\begingroup$ The code runs fine though. $\endgroup$
    – akhileshsk
    Feb 6, 2015 at 1:17
  • $\begingroup$ @akhileshsk You can enter pretty much anything into Mathematica and it "runs" but it might not give you what you want. We could figure out why it gives you the result it gives, but I don't think it's worth one's time. See point 3.2 here for an explanation. The message you get when you load Combinatorica is a warning that this package is deprecated, this will always happen when Combinatorica is loaded. $\endgroup$
    – Szabolcs
    Feb 6, 2015 at 1:32
  • $\begingroup$ The error message appeared without any parallelization and the answer was just what I wanted. Thanks for your help. $\endgroup$
    – akhileshsk
    Feb 6, 2015 at 1:36
  • $\begingroup$ @akhileshsk Yes, the warning is unrelated to parallelization. $\endgroup$
    – Szabolcs
    Feb 6, 2015 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.