2
$\begingroup$
Manipulate[
m = RandomPrime[d];
{m, d},
{d, {20, 50}, SetterBar}
] 

Basically, I expected the code to generate a random prime m (under the upper bound d) for me to make some graphics about m. However, the piece of code evaluates m multiple times, which is bewildering to me. Probably I need to use Dynamic here, but I have not figured it out.

How can I evaluate the RandomPrime function for only once?

$\endgroup$
2
$\begingroup$

whenever you use Manipulate, use TrackedSymbols to tell it which symbols to track

Manipulate[m = RandomPrime[d];
 {m, d}, {d, {20, 50}, SetterBar},
 TrackedSymbols :> {d}]

Without this, it will track each symbol in its expression and if any changes, it will re-evaluate again automatically until no more changes are detected.

| improve this answer | |
$\endgroup$

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.