Skip to content

Early error if RNG is used when it shouldn't #832

Description

@HenrikBengtsson

We can detect RNG misuse by setting options(future.rng.onMisuse = "error"), e.g.

library(future)
options(future.rng.onMisuse = "error")
f <- future(sample.int(3))
v <- value(f)
#> Error: UNRELIABLE VALUE: Future (<unnamed-2>) 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 <unnamed-2> (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.

> 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").

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions