1
$\begingroup$

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

Easy peasy.

With[{t = Test}, t + t]
$\endgroup$
1
  • $\begingroup$ Perfect! Thank you! $\endgroup$ Commented Jan 3, 2018 at 14:31

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.