bbarker
bbarker

Reputation: 13098

How to prevent (Random) expressions in a watch expression from being cached, or re-evaluate them?

Alternatively, is there a way to force a re-evaluation of a single watch expression?

Say I have the following watch expression:

> Random.splitmix 123 '(Random.natIn 0 100)

When I run this, I might see a result like:

  Now evaluating any watch expressions (lines starting with `>`)... Ctrl+C cancels.


    5 | > Random.splitmix 123 '(Random.natIn 0 100)
          ⧩
          56

Saving the file again will show the same result every time (it is cached).

I'm not sure if Random results should never be cached (maybe that's still good default behavior to save on computation time), but just wondering what the best workarounds are for this.

debug.clear-cache doesn't work either in this situation, since each time the RNG (Random.splitmix) starts over with the same seed.

Of course, we can manually change the random seed, but this also may not always be desired behavior (and a minor nitpick would be that it involves unnecessary keystrokes and creates additional caching - one cached result per seed, so you have to recall which seeds you already used).

Upvotes: 1

Views: 77

Answers (2)

bbarker
bbarker

Reputation: 13098

Since the watch expression would somehow need to maintain the random state, which is likely more trouble than it is worth, manually editing the random seed is likely the best compromise. Just re-evaluating will always start from the initial value produced from the given random seed.

Alternatively or conjunctively, evaluating a list of random values may be useful.

Upvotes: 0

Apocalisp
Apocalisp

Reputation: 35054

You can clear the expression cache with debug.clear-cache in UCM.

That said, re-evaluating your expression is actually going to give the same result every time! The splitMix function is completely deterministic, so the result you get depends on the seed you provide and on nothing else.

So you could clear the cache here, but it’s not going to do anything.

To get a really random value, you need to use IO which is not allowed in watch expressions. You’d need to provide I/O to your program using run in UCM.

Upvotes: 4

Related Questions