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)
Needs["Combinatorica`"]
MakeRow[n_, l_, p_] :=
Module[{v = RandomComposition[p, n]},
While[Max[v] > l, v = RandomComposition[p, n]];
v]
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
Answer
Packages need to be loaded explicitly for parallel kernels using ParallelNeeds
.
Just do ParallelNeeds["Combinatorica`"]
before running the code.
Comments
Post a Comment