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

Setting random seed in R #25

Closed
matherealize opened this issue Aug 1, 2023 · 2 comments
Closed

Setting random seed in R #25

matherealize opened this issue Aug 1, 2023 · 2 comments

Comments

@matherealize
Copy link

matherealize commented Aug 1, 2023

I'm interested in using your package for simulations. However, at the moment it seems that setting a random seed requires a direct call to Julia.

In the readme it is shown to use "set.seed()" in R, but in my example this has no effect since the seed is not passed to Julia.

> bs = bigsimr::bigsimr_setup()
> set.seed(1)
> bs$rmvn(2, rep(0, 5), diag(1, 5))
          [,1]       [,2]       [,3]       [,4]       [,5]
[1,] 0.8107133  0.5801268 -0.9684956 -0.2439811  0.4141377
[2,] 0.1580521 -0.1109701 -0.3932944 -0.7755616 -0.5166628
> set.seed(1)
> bs$rmvn(2, rep(0, 5), diag(1, 5))
         [,1]      [,2]      [,3]       [,4]        [,5]
[1,] 1.027204  1.141811 -1.251099 0.08030231 -0.05882845
[2,] 1.694608 -1.659026 -1.812908 0.06768497  0.34145174

It DOES work if I set the seed in Julia myself:

> rnd = JuliaCall::julia_pkg_import("Random", "Random.seed!")
> rnd$`Random.seed!`(as.integer(1))
Julia Object of type MersenneTwister.
MersenneTwister(UInt32[0x00000001], Random.DSFMT.DSFMT_state(Int32[1749029653, 1072851681, 1610647787, 1072862326, 1841712345, 1073426746, -198061126, 1073322060, -156153802, 1073567984  …  1977574422, 1073209915, 278919868, 1072835605, 1290372147, 18858467, 1815133874, -1716870370, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000  …  0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)
> bs$rmvn(2, rep(0, 5), diag(1, 5))
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -0.3170409 -0.5091629  0.1345132  1.0872089 -0.3866632
[2,] -0.4261330  0.1658788 -0.5986539 -0.9809587 -0.1185315
> rnd$`Random.seed!`(as.integer(1))
Julia Object of type MersenneTwister.
MersenneTwister(UInt32[0x00000001], Random.DSFMT.DSFMT_state(Int32[1749029653, 1072851681, 1610647787, 1072862326, 1841712345, 1073426746, -198061126, 1073322060, -156153802, 1073567984  …  1977574422, 1073209915, 278919868, 1072835605, 1290372147, 18858467, 1815133874, -1716870370, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000  …  0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)
> bs$rmvn(2, rep(0, 5), diag(1, 5))
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -0.3170409 -0.5091629  0.1345132  1.0872089 -0.3866632
[2,] -0.4261330  0.1658788 -0.5986539 -0.9809587 -0.1185315

It would be great if this could be updated / implemented. Maybe documenting this is already enough as well. Thank you for your work!

Version: 0.11.2 from CRAN
R: 4.1.1

@adknudson
Copy link
Member

Hi @matherealize , thanks for opening the issue. You are correct that setting the seed requires a direct call to Julia. For now, I will update the readme to show the proper way to set the seed. Do note that it is probably still a good idea to set the seed in R at the same time to ensure reproducibility.

@matherealize
Copy link
Author

I see, thank you for the quick clarification! I will close this for now.

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

2 participants