Can I get.seed() somehow?

后端 未结 4 1450
不知归路
不知归路 2020-11-30 09:55

In reference to the statement set.seed(), can I get the seed instead after running some code if I didn\'t set it explicitly?

I\'ve been re-running some

4条回答
  •  佛祖请我去吃肉
    2020-11-30 10:08

    To add to the answer mpettis gave, if you don't want to re-execute the script manually--generating new random seeds each iteration--you could do something like this:

    # generate vector of seeds
    eff_seeds <- sample(1:2^15, runs)
    
    # perform 'runs' number of executions of your code
    for(i in 1:runs) {
        print(sprintf("Seed for this run: %s", eff_seeds[i]))
        set.seed(eff_seeds[i])
    
        # your code here
        # don't forget to save your outputs somehow
    }
    

    Where the variable 'runs' is a positive integer indicating the number of times you want to run your code.

    This way you can generate a lot of output rapidly and have individual seeds for each iteration for reproducibility.

提交回复
热议问题