{"action":"created","issue":{"url":"https://api.github.com/repos/rpruim/mosaic/issues/431","labels_url":"https://api.github.com/repos/rpruim/mosaic/issues/431/labels{/name}","comments_url":"https://api.github.com/repos/rpruim/mosaic/issues/431/comments","events_url":"https://api.github.com/repos/rpruim/mosaic/issues/431/events","html_url":"https://github.com/rpruim/mosaic/issues/431","id":60965828,"number":431,"title":"do() doesn't respect set.seed()?","user":{"login":"beanumber","id":2774211,"avatar_url":"https://avatars.githubusercontent.com/u/2774211?v=3","gravatar_id":"","url":"https://api.github.com/users/beanumber","html_url":"https://github.com/beanumber","followers_url":"https://api.github.com/users/beanumber/followers","following_url":"https://api.github.com/users/beanumber/following{/other_user}","gists_url":"https://api.github.com/users/beanumber/gists{/gist_id}","starred_url":"https://api.github.com/users/beanumber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beanumber/subscriptions","organizations_url":"https://api.github.com/users/beanumber/orgs","repos_url":"https://api.github.com/users/beanumber/repos","events_url":"https://api.github.com/users/beanumber/events{/privacy}","received_events_url":"https://api.github.com/users/beanumber/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-13T02:38:28Z","updated_at":"2015-03-17T19:01:19Z","closed_at":null,"body":"```{r}\r\nhead(sample(1:100))\r\n```\r\n\r\nThe first time I did this, I got:\r\n\r\n64, 86, 31, 93, 52, 75, ...\r\n\r\nBut, I get a different thing everytime I knit this, as expected.\r\n\r\n```{r}\r\nset.seed(50)\r\nhead(sample(1:100))\r\n```\r\n\r\nNow I get the **same thing** every time I knit this, as expected. \r\n\r\n71, 44, 20, 75, 50, 5, ...\r\n\r\n```{r, message=FALSE}\r\nrequire(mosaic)\r\nrequire(mosaicData)\r\nbstrap <- do(1000) * cor(length ~ width, data=resample(KidsFeet))\r\nfavstats(bstrap)\r\n```\r\n\r\nWhen I knit this the first time, the mean was 0.6320324. **Why does it change every time I knit**!! Shouldn't it be the same?\r\n\r\n```{r}\r\nhead(resample(KidsFeet))\r\n```\r\n\r\nJosh, Edward, Mark, ...\r\nThis seems to be working as expected.\r\n\r\n```{r}\r\nhead(sample(1:100))\r\n```\r\n\r\n55, 16, 26, 17, 5, ...\r\n\r\nBase R is still returning the same sequence of random numbers. \r\n\r\nSo the problem seems to be with `do()`. Any idea what is going on here? "},"comment":{"url":"https://api.github.com/repos/rpruim/mosaic/issues/comments/82531302","html_url":"https://github.com/rpruim/mosaic/issues/431#issuecomment-82531302","issue_url":"https://api.github.com/repos/rpruim/mosaic/issues/431","id":82531302,"user":{"login":"rpruim","id":722231,"avatar_url":"https://avatars.githubusercontent.com/u/722231?v=3","gravatar_id":"","url":"https://api.github.com/users/rpruim","html_url":"https://github.com/rpruim","followers_url":"https://api.github.com/users/rpruim/followers","following_url":"https://api.github.com/users/rpruim/following{/other_user}","gists_url":"https://api.github.com/users/rpruim/gists{/gist_id}","starred_url":"https://api.github.com/users/rpruim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rpruim/subscriptions","organizations_url":"https://api.github.com/users/rpruim/orgs","repos_url":"https://api.github.com/users/rpruim/repos","events_url":"https://api.github.com/users/rpruim/events{/privacy}","received_events_url":"https://api.github.com/users/rpruim/received_events","type":"User","site_admin":false},"created_at":"2015-03-17T19:01:18Z","updated_at":"2015-03-17T19:01:18Z","body":"If the `parallel` package is installed, then it is used to speed things up.  Random number generation in a parallel environment is a bit different because one needs to think about how child processes generate random values with respect to parents and siblings.\r\n\r\nSo you have two options:\r\n\r\n * turn off `parallel` using `do(1000, parallel=FALSE) *`\r\n * tell the `parallel` package that you want reproducibility:\r\n\r\n```\r\n> RNGkind(\"L'Ecuyer-CMRG\")\r\n> set.seed(123); parallel::mc.reset.stream(); do(2) * head(resample(KidsFeet), 3)\r\n       name birthmonth birthyear length width sex biggerfoot domhand orig.ids .row .index\r\n35    Peter          4        88   24.7   8.6   B          R       L       35    1      1\r\n9   Eleanor          5        88   23.6   9.3   G          R       R        9    2      1\r\n22     Josh          7        88   24.4   8.6   B          L       R       22    3      1\r\n37 Teshanna          3        88   26.0   9.0   G          L       R       37    1      2\r\n16     Kate          4        88   23.7   7.9   G          R       R       16    2      2\r\n36   Hannah          3        88   22.9   8.5   G          L       R       36    3      2\r\n> set.seed(123); parallel::mc.reset.stream(); do(2) * head(resample(KidsFeet), 3)\r\n       name birthmonth birthyear length width sex biggerfoot domhand orig.ids .row .index\r\n35    Peter          4        88   24.7   8.6   B          R       L       35    1      1\r\n9   Eleanor          5        88   23.6   9.3   G          R       R        9    2      1\r\n22     Josh          7        88   24.4   8.6   B          L       R       22    3      1\r\n37 Teshanna          3        88   26.0   9.0   G          L       R       37    1      2\r\n16     Kate          4        88   23.7   7.9   G          R       R       16    2      2\r\n36   Hannah          3        88   22.9   8.5   G          L       R       36    3      2\r\n```\r\nI suppose we could consider doing something to wrap up the call to `mc.reset.stream()`, but I don't think there is any way to get `set.seed()` by itself to do what you were expecting when using parallel random number generation.\r\n\r\nSee the help for `mcparallel` and `mclapply` for more info."}}