Why does setting the seed in one mirai_map()
use the same seed in another one?
#271
-
Hi, I came across some weird behavior related to randomness and I didn’t find related mentions in the docs (I looked for “random” and “seed”). In the example below, it appears that setting the seed in a library(mirai)
library(parallel)
packageVersion("mirai")
#> [1] '2.2.0.9001'
cl <- make_cluster(4)
daemons(4)
#> [1] 4
vec <- 1:3
vec2 <- 4:6
# Returns different values: good
mirai_map(list(vec, vec2), \(x) rnorm(x))[]
#> [[1]]
#> [1] 0.07040429 0.40971925 -0.39779374
#>
#> [[2]]
#> [1] -0.5323828 0.1015743 -0.5020980
# Set the seed in the function
mirai_map(list(vec, vec2), \(x) {
set.seed(123)
rnorm(x)
})[]
#> [[1]]
#> [1] -0.9685927 0.7061091 1.4890213
#>
#> [[2]]
#> [1] -0.9685927 0.7061091 1.4890213
# Do not set the seed in the function: still identical results?
mirai_map(list(vec, vec2), \(x) rnorm(x))[]
#> [[1]]
#> [1] -1.8150926 0.3304096 -1.1421557
#>
#> [[2]]
#> [1] -1.8150926 0.3304096 -1.1421557 Maybe I come with the wrong priors but I though that “what happens in the daemons(0)
#> [1] 0
daemons(4, dispatcher = FALSE)
#> [1] 4
mirai_map(list(vec, vec2), \(x) {
set.seed(123)
rnorm(x)
})[]
#> [[1]]
#> [1] -0.9685927 0.7061091 1.4890213
#>
#> [[2]]
#> [1] -0.9685927 0.7061091 1.4890213
mirai_map(list(vec, vec2), \(x) rnorm(x))[]
#> [[1]]
#> [1] -1.7628219 0.1110283 0.3758775
#>
#> [[2]]
#> [1] 0.1192312 -0.4474403 -1.9538161 Should this be mentioned more prominently in the docs (unless I missed it)? In any case, thanks for this package! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Attempting to set the seed in each iteration of a A bit more about why the behaviour is like this: mirai uses L'Ecuyer CMRG to provide parallel-safe random streams. That means random results in each daemon are independent from one another. The random seed is not reset after each mirai call by design. This is to ensure that however many random draws are made in any mirai call, the next random draw follows on in the stream, and hence have the desired statistical properties. Once you set the seed in a mirai call, then subsequent calls will follow on, hence your findings. It's nothing to do with dispatcher btw, if you set If you're able to give some context about what you're attempting, and what you end up doing, that would help me add something in the docs to help others. Thanks. |
Beta Was this translation helpful? Give feedback.
Attempting to set the seed in each iteration of a
mirai_map()
call is probably not what you'd want, as you get the same random results (as you found).A bit more about why the behaviour is like this: mirai uses L'Ecuyer CMRG to provide parallel-safe random streams. That means random results in each daemon are independent from one another. The random seed is not reset after each mirai call by design. This is to ensure that however many random draws are made in any mirai call, the next random draw follows on in the stream, and hence have the desired statistical properties.
Once you set the seed in a mirai call, then subsequent calls will follow on, hence your findings.
It's nothing to do wi…