futureverse / futureverse/future
Early error if RNG is used when it shouldn't
- Dominant language
- R
- Stars
- 1k
- Forks
- 92
- PR merge metrics
- No merged PRs in 30d
Description
We can detect RNG misuse by setting `options(future.rng.onMisuse = "error")`, e.g.
```r
library(future)
options(future.rng.onMisuse = "error")
f <- future(sample.int(3))
v <- value(f)
#> Error: UNRELIABLE VALUE: Future () unexpectedly generated random numbers
without specifying argument 'seed'. There is a risk that those random numbers are not statistically
sound and the overall results might be invalid. To fix this, specify 'seed=TRUE'. This ensures that
proper, parallel-safe random numbers are produced. To disable this check, use 'seed=NULL', or set
option 'future.rng.onMisuse' to "ignore". [future (54b70f088e2c65b835aea5f0cd100eff-2);
on 54b70f088e2c65b835aea5f0cd100eff@hb-x1-2023<867157>]
```
This is great, but it only detects RNG misuse at the end very end when the future has been resolved. If the RNG misuse takes place early on, it's a waste of compute resource.
# Idea
Write a custom RNG that generates an instant error if the RNG is used, e.g.
```r
> rng_allow(FALSE)
> sample.int(3)
Error in sample.int(...) : Unexpected RNG call detected
> rnorm(1)
Error in sample.int(...) : Unexpected RNG call detected
> rng_allow(TRUE)
[1] 2 1 3
```
Futures can set this internally when `getOption("future.rng.onMisuse") is `"error"`. The outcome is earlier stopping and therefore also less wasted compute resources.
Custom RNGs can be implemented in C and registered as [`RNGkind("user-supplied")`](https://search.r-project.org/R/refmans/base/html/Random-user.html).
Contributor guide
Assessment
This issue has not been assessed yet.