Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the same random result across purrr::map() and furrr::future_map() #251

Open
DanChaltiel opened this issue Feb 22, 2023 · 3 comments

Comments

@DanChaltiel
Copy link

Hi,

I am running simulations where some computing should be parallelized and some should not, and I am trying to figure out how to ensure that purrr::map() and furrr::future_map() yield the same result for a given seed.

Reading the help of furrr_options(), I'm trying to figure out how to pass the signs to future_map(), but this problem is not addressed specifically.

For instance, consider the following code:

library(purrr)
library(furrr)

set.seed(42)
rnorm(2)
#> [1]  1.3709584 -0.5646982

set.seed(42)
map_dbl(1:2, ~rnorm(1))
#> [1]  1.3709584 -0.5646982

set.seed(42)
future_map_dbl(1:2, ~rnorm(1), .options=furrr_options(seed = list(.Random.seed, .Random.seed)))
#> [1] 1.370958 1.370958

set.seed(42)
future_map_dbl(1:2, ~rnorm(1), .options=furrr_options(seed = list(.Random.seed[1:10], .Random.seed[2:11])))
#> Error in sample.int(n = 1L, size = 1L, replace = FALSE): '.Random.seed' has wrong length

Created on 2023-02-22 with reprex v2.0.2

Using seed=FALSE, seed=TRUE, seed=NULL, or even seed=42L yielded different results (or errors), but none was right.

Is there a way to pass the right seed to each parallel iteration of furrr::future_map() so that it yields the same result as purrr::map()?

If yes, it might be worth clarifying in the documentation, and if not, could this be considered a new feature?

@HenrikBengtsson
Copy link

This question was also asked at https://stackoverflow.com/q/75521357/1072091. I've answered it at https://stackoverflow.com/a/75543379/1072091.

@DanChaltiel
Copy link
Author

Thank you very much for your answer on SO, this was very helpful and interesting.

I still think this should be mentioned in the documentation though.

@DavisVaughan
Copy link
Owner

The docs in ?furrr::future_options() for Reproducible random number generation (RNG) mention that random numbers are the same regardless of the parallel backend, but dont mention that it would be different from purrr::map(), so I can add a sentence about that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants