I would like to call a function, which creates some random output (let's say a random integer) in a single expression twice. A minimal example:

Test := RandomInteger[2]
SumTest = Test + Test

Here, the both values "Test" in the second line are different. I want them to be the same, i.e. the output of the second line should be 0, 2 or 4 instead of 0, 1, 2, 3 or 4.


Easy peasy.

With[{t = Test}, t + t]
| improve this answer | |
  • $\begingroup$ Perfect! Thank you! $\endgroup$ – QuantumMechanics Jan 3 '18 at 14:31

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.