Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:32:08+00:00 2026-06-07T08:32:08+00:00

R ignores setting .Random.seed inside of an lapply. Using set.seed however, works fine. Some

  • 0

R ignores setting .Random.seed inside of an lapply. Using set.seed however, works fine.

Some code:

# I can save the state of the RNG for a few seeds
seed.list <- lapply( 1:5, function(x) {
                        set.seed(x)
                        seed.state <- .Random.seed
                        print( rnorm(1) )
                        return( seed.state )}) 
#[1] -0.6264538
#[1] -0.8969145
#[1] -0.9619334

# But I get different numbers if I try to restore 
# the state of the RNG inside of an lapply
tmp.rest.state <-  lapply(1:5, function(x) { 
                        .Random.seed <- seed.list[[x]]
                        print(rnorm(1))})
# [1] -0.2925257
# [1] 0.2587882
# [1] -1.152132

# lapply is just ignoring the assignment of .Random.seed
.Random.seed <- seed.list[[3]]
print( rnorm(1) ) # The last printed value from seed.list
# [1] -0.9619334
print( rnorm(1) ) # The first value in tmp.rest.state
# [1] -0.2925257

My goal is to checkpoint MCMC runs so that they can be resumed exactly. I can easily save the state of the RNG, I just can’t get R to load it inside of an lapply loop!

Is there a way to force R to notice setting .Random.seed? Or is there a simpler way to make this happen?

In case it matters, I’m using 64 bit R:

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Platform: x86_64-pc-linux-gnu (64-bit)

On Ubuntu 12.04 LTS:

nathanvan@nathanvan-N61Jq:~$ uname -a
Linux nathanvan-N61Jq 3.2.0-26-generic #41-Ubuntu SMP Thu Jun 14 17:49:24 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:32:13+00:00Added an answer on June 7, 2026 at 8:32 am

    This happens because .Random.seed is evaluated as a local object inside your call to lapply.

    You need to assign the value of .Random.seed in the global environment:

    tmp.rest.state <- lapply(seed.list, function(x) {
        assign(".Random.seed", x, envir=globalenv())
        print(rnorm(1))
      }
    )
    
    [1] -0.6264538
    [1] -0.8969145
    [1] -0.9619334
    [1] 0.2167549
    [1] -0.8408555
    

    The reason your code doesn’t work is that .Random.seed get assigned in the environment of the anonymous function in lapply, but rnorm() looks for .Random.seed in the global environment.


    For the record, here is my first attempt, which will only work in some situations:

    Here is one way to fix it, by using <<-. (Yes, I know this is frowned upon, but possibly justified. An alternative would be to use eval() and force evaluation in a calling environment.

    tmp.rest.state <- lapply(seed.list, function(x) {
        .Random.seed <<- x
        print(rnorm(1))
      }
    )
    
    [1] -0.6264538
    [1] -0.8969145
    [1] -0.9619334
    [1] 0.2167549
    [1] -0.8408555
    

    Note that this solution will not work if your lapply() is nested in another function, since <<- only evaluates in the parent environment, not the global environment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reviewing a part of code using php mcrypt library to encrypt some
Eclipse can be configured to ignore unavoidable generic type problems when setting the preferences
This code only renders a dodecahedron and completely ignores the glBegin(GL_TRIANGLES) block: glutSolidDodecahedron(); glBegin(GL_TRIANGLES);
How can I make a linq search that ignores nulls (or nullables)? I have
When using Linq-to-SQL, adding a column to an existing table, and setting a default
I am trying to set custom font to Paragraph, but I can't make it
I'm having some trouble setting the quality settings on a video via the Youtube
I'm using GetOpenFileName and setting the lpstrInitialDir member of the OPENFILENAME struct to the
How would one go about setting up some hotkeys for commonly used SVN actions
How do I make curl ignore the proxy? Setting $NO_PROXY doesn't seem to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.