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$
  • 2
    $\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$ – Fred Simons Nov 30 '15 at 14:53
  • $\begingroup$ Related example in the documentation. $\endgroup$ – Karsten 7. Nov 30 '15 at 15:25
  • $\begingroup$ I hope this is not marked as on hold:-)! $\endgroup$ – Dimitris Nov 30 '15 at 15:27
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$
  • $\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 '15 at 15:20
  • $\begingroup$ @MarcoB Thanks! Fixed it. $\endgroup$ – Karsten 7. Nov 30 '15 at 15:23
  • $\begingroup$ Why the first image contains fewer cylinders? $\endgroup$ – Dimitris Nov 30 '15 at 15:26
  • $\begingroup$ @dimitris Because all kernels produce the same cylinders. $\endgroup$ – Karsten 7. Nov 30 '15 at 15:28
  • $\begingroup$ @dimitris Compare for example Length@cylinders with Length@DeleteDuplicates@cylinders. $\endgroup$ – Karsten 7. Nov 30 '15 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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