Here is an attempt to fix the problem of not having an inverse getSeed function for the setSeed function. I posted a similar question about twelve hours ago on Using R, how to get.seed()? which was closed as it is classified a "duplicate" ...
I have "hacked" together a solution with a seed memory which requires a global variable .random.seed.memory.
utils::globalVariables(c(".random.seed.memory"));
Timings are important since I have to "generate a seed" using set.seed
github.monte = "https://raw.githubusercontent.com/MonteShaffer/";
include.me = paste0(github.monte, "humanVerse/main/humanVerse/R/functions-str.R");
source(include.me); # trimMe function
include.me = paste0(github.monte, "humanVerse/main/humanVerse/R/functions-random.R");
source(include.me); # getSeed, setSeed, and so on.
The function setSeed behaves generally like set.seed but any custom parameters passed to set.seed beyond the integer (kind, normal.kind, sample.kind) need to be listed in args.set as the ellipses ... for setSeed are used to pass parameters to initSeed(...) an internal function that enables setSeed and getSeed to work.
I also wrote a C-standard rand() function that passes in a min, max, n, method, and so on. This is how I generate an "integer" to feed the setSeed and store in memory. I use Sys.time() as min/max for a default seed generation (min = -1*as.integer(Sys.time()) and max = as.integer(Sys.time()) ). sample is a bad idea as it has to create a vector in the range to compute a single value, but it is an method option of rand() which feeds initSeed. The default "high-low" I found to be slightly faster than "floor".
USAGE
### VERBOSITY is HIGH AT THE MOMENT ###
print("random 5"); rnorm(5);
setSeed(NULL); # this will automatically call initSeedMemory() if necessary
setSeed(.random.seed.memory$last); rnorm(5);
setSeed(getSeed()); rnorm(5);
print("random 5"); rnorm(5);
setSeed(getSeed()); rnorm(5);
By default it stores the seed value to an element in the global list called "last" ... This enables you to keep track of different memory seeds depending on the processes you are running. In the example below I access "last" specifically and "nsim" ... a second seed stored in memory ...
### VERBOSITY is HIGH AT THE MOMENT ###
initSeedMemory( purge.memory = TRUE);
setSeed(NULL);
setSeed(.random.seed.memory$last); rnorm(5);
setSeed(getSeed()); rnorm(5);
getSeed(); # accessor to .random.seed.memory
.random.seed.memory;
print("random 5"); rnorm(5);
setSeed(NULL, key="nsim"); rnorm(5);
setSeed(.random.seed.memory$nsim, key="nsim"); rnorm(5);
setSeed(getSeed("nsim"), key="nsim"); rnorm(5);
getSeed("nsim"); # accessor to .random.seed.memory
.random.seed.memory;
print("random 5"); rnorm(5);
setSeed(.random.seed.memory$last); rnorm(5);
setSeed(getSeed()); rnorm(5);
.random.seed.memory;
set.seed(.random.seed.memory$last); rnorm(5);
set.seed(.random.seed.memory$nsim); rnorm(5);
.random.seed.memory;
print("random 5"); rnorm(5);
Of course, it may have bugs, and I would appreciate any suggestions or bugs found.
-- 02/19/2021 about 5AM in PST --
Of course, the ability to pass in a fixed seed is also possible.
setSeed(NULL, "nsim"); rnorm(5); # randomly generated seed under the hood, but stored
setSeed(123, "z5"); rnorm(5); # you can still fix the seed manually yourself, still stored and accessible in the list
setSeed(getSeed("nsim"), "nsim"); rnorm(5);
setSeed(getSeed("z5"), "z5"); rnorm(5);