jeudi 18 février 2021

Using R, how to get.seed()? [duplicate]

I run a lot of simulations, and we want them to be random, yet replicable.

I would like to be able to get.seed() after I perform an operation. I would like the value to return to be a numeric integer that I could later call as set.seed(NNNN);

In mathematics, there is a fundamental principle of inverses in algebra, and it also relates to programming algebra. For example:

N = 3;
pnorm(N);
qnorm( pnorm( N ) );

Where is the inverse function for set.seed()? Where is get.seed()?

So with set.seed I can pass in a NULL entry and it will "generate" a random seed for the random-ness to follow (which dies after some "unspecified" time).

rnorm(5);
set.seed(NULL); rnorm(5);
set.seed(123); rnorm(5);
set.seed(NULL); rnorm(5);
set.seed(123); rnorm(5);

UNDER the hood, there is a global variable that looks like some sort of digest object of integers:

set.seed(NULL); .Random.seed;
set.seed(123); .Random.seed;

So where is the reverse mapping of .Random.seed to the INTEGER that is input NNNN?

Where is get.seed()?

Certainly I can randomly pick a value for NNNN and store and report, but that is me "introducing" an extra level of random-ness in the research and simulation that I may not want.

Under the hood, when NULL was passed, the seed has to be generated. Is it going directly to the "Mersenne-Twister" without generating a NNNN value first? Is it not reversible?

It is "as-if" I was given a function deg2rad without its inverse rad2deg. Is there something under the hood I am missing?

Has someone written a get.seed() function in custom form to address this issue. Something like this "other missing function" in BaSe R.

#' rgb2col
#'
#' Reverse the built-in grDevices::col2rgb function
#'
#' @param x vector of colors
#'
#' @return vector of colors in RGB hex format
#' @export
#'
#' @examples
#'
#' rgb2col( col2rgb("red") );
#' rgb2col( col2rgb("red", alpha=TRUE) );
#' rgb2col( col2rgb("#FF0000FF", alpha=TRUE) );
#' rgb2col( col2rgb("#FF000033", alpha=TRUE) );
#'
rgb2col = function(x)
  {
  # reverses col2rgb function
  x.n = dim(x)[1];
  if(x.n == 4)
    {
    x.rgb = t(x[1:4,]) /255;
    grDevices::rgb(   as.numeric(x.rgb[,1]),
                      as.numeric(x.rgb[,2]),
                      as.numeric(x.rgb[,3]),
                      as.numeric(x.rgb[,4]),
      names=rownames(x.rgb) );
    } else {
            x.rgb = t(x[1:3,]) /255;
            grDevices::rgb( x.rgb, names=rownames(x.rgb) );
            }
  }



Aucun commentaire:

Enregistrer un commentaire