6
$\begingroup$

Each time I execute the following code it generates the same cylinders with the same format.

SeedRandom[157]
cylinders = 
  Table[{RandomReal[{-100, 100}, {2, 3}], RandomReal[5]}, {50}];
SeedRandom[157]
ImageCrop[
 Graphics3D[{EdgeForm[None], 
     Directive[Opacity@RandomReal[{.4, .9}], Hue[RandomReal[]]], 
     Cylinder[First@#, Last@#]} & /@ cylinders, Boxed -> False, 
  ImageSize -> 800]]

enter image description here However,

SeedRandom[157]
cylinders = 
  ParallelTable[{RandomReal[{-100, 100}, {2, 3}], 
    RandomReal[5]}, {50}];
SeedRandom[157]
ImageCrop[
 Graphics3D[{EdgeForm[None], 
     Directive[Opacity@RandomReal[{.4, .9}], Hue[RandomReal[]]], 
     Cylinder[First@#, Last@#]} & /@ cylinders, Boxed -> False, 
  ImageSize -> 800]]

enter image description here

does not exhibit the same behavior. I guess I am missing something fundamental here.

$\endgroup$
3
  • 3
    $\begingroup$ SeedRandom is only set for the main kernel, not for the subkernels. Even when you would set it in each subkernel, I think you will get a result different from the first one. $\endgroup$ Nov 30, 2015 at 14:53
  • $\begingroup$ Related example in the documentation. $\endgroup$
    – Karsten7
    Nov 30, 2015 at 15:25
  • $\begingroup$ I hope this is not marked as on hold:-)! $\endgroup$
    – Dimitris
    Nov 30, 2015 at 15:27

1 Answer 1

6
$\begingroup$
ParallelEvaluate[SeedRandom[157]]
cylinders = 
  ParallelTable[{RandomReal[{-100, 100}, {2, 3}], 
    RandomReal[5]}, {50}];
SeedRandom[157]
ImageCrop[
 Graphics3D[{EdgeForm[None], 
     Directive[Opacity@RandomReal[{.4, .9}], Hue[RandomReal[]]], 
     Cylinder[First@#, Last@#]} & /@ cylinders, Boxed -> False, 
  ImageSize -> 800]]

enter image description here

ParallelEvaluate[SeedRandom[157 + $KernelID]]
cylinders = 
  ParallelTable[{RandomReal[{-100, 100}, {2, 3}], 
    RandomReal[5]}, {50}];
SeedRandom[157]
ImageCrop[
 Graphics3D[{EdgeForm[None], 
     Directive[Opacity@RandomReal[{.4, .9}], Hue[RandomReal[]]], 
     Cylinder[First@#, Last@#]} & /@ cylinders, Boxed -> False, 
  ImageSize -> 800]]

enter image description here

$\endgroup$
6
  • $\begingroup$ Karsten, note that evaluating the second SeedRandom in parallel doesn't keep the coloring repeatable. I think that the second call should simply be to the local master kernel, i.e. no ParallelEvaluate there. $\endgroup$
    – MarcoB
    Nov 30, 2015 at 15:20
  • $\begingroup$ @MarcoB Thanks! Fixed it. $\endgroup$
    – Karsten7
    Nov 30, 2015 at 15:23
  • $\begingroup$ Why the first image contains fewer cylinders? $\endgroup$
    – Dimitris
    Nov 30, 2015 at 15:26
  • $\begingroup$ @dimitris Because all kernels produce the same cylinders. $\endgroup$
    – Karsten7
    Nov 30, 2015 at 15:28
  • $\begingroup$ @dimitris Compare for example Length@cylinders with Length@DeleteDuplicates@cylinders. $\endgroup$
    – Karsten7
    Nov 30, 2015 at 15:30

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.