Suppose I have the following functions:

f1:=(a=RandomInteger[{1,24}]; b=RandomInteger[{1,24}]; Return[a+b])
f2:=If[f1==2,Print["snake eyes"]; Return[true], Print["nada"]; Return[false]]
  1. How can I parallelize these functions so that each kernel continuously executes f1 until any one kernel obtains "snake eyes" (at which point the process should terminate)?
  2. Relating to the first question, once parallelized is there a more efficient way of writing or executing this resulting code?
  • $\begingroup$ For a discussion of interkernel communication, the primitives, and the issues that arise, I think this question is quite informative (the question AND the answer, both). $\endgroup$
    – Igor Rivin
    Sep 22, 2014 at 0:17

1 Answer 1


All function you need it's DistributeDefinitions and ParallelTry

f1 := RandomInteger[{1, 24}] + RandomInteger[{1, 24}];
func[x_] := While[True,
   If[f1 === 2,
    Print["snake eyes"];

ParallelTry[func, Range[$KernelCount]]

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.