How to Get.Seed() Somehow

Can I get.seed() somehow?

If you didn't keep the seed, there's no general way to "roll back" the random number generator to a previous state after you've observed a random draw. Going forward, what you may want to do is save the value of .Random.seed along with the results of your computations. Something like this.

x <- .Random.seed
result <- <your code goes here>
attr(result, "seed") <- x

Then you can reset the PRNG as follows; result2 should be the same as result.

.Random.seed <- attr(result, "seed")
result2 <- <your code goes here>

How to get seed of current state of random generator in goal to place it in set.seed() function

Call set.seed(x) at the beginning of each iteration. Make sure you can identify the seed that was used before you started the process, so that you can use it later. For example:

for (seed in seeds) {
set.seed(i)
print(sprintf('using seed = %d\n', seed))
do_your_stuff(...)
}

In a comment you asked:

how to choose seed in proper manner - shouldn't it be some "random" prime numbers not the simple series of integers (if we talk about vector of containing seeds) ?

I'm not sure how it matters if seeds is simply a sequence (like 1:100) or random prime numbers. As far as I know, any seed number X is just as good as any other Y. But if that's important to you, then you can grab a list of prime numbers from somewhere (for example here) and use sample to randomize them, for example:

seeds <- sample(c(7, 17, 19, 23, 1019, 1021))

Print the current random seed so that I can enter it with set.seed() later

There's effectively a one-way relationship between the seed used in set.seed() and the information in .Random.seed; as ?Random.seed says and @MattTenenbaum's answer shows, the information in .Random.seed can be saved and restored. I appreciate the desire for a function that would derive a simple integer seed from the current state of .Random.seed, but in its absence, you have to save and restore the full information ... for example

set.seed(1001)
save(".Random.seed",file="random_state_seed1001.RData") ## save current state
runif(1)
## [1] 0.9856888
runif(1)
## [1] 0.4126285
runif(1)
## [1] 0.4295392
load("random_state_seed1001.RData") ## restore state just after set.seed()
runif(1)
## [1] 0.9856888

As @JoshuaUlrich points out, this only works/is only safe if you don't modify the type of RNG (and type of normal deviate generator) between saving and restoring ...

A more complete solution:

save_rng <- function(savefile=tempfile()) {
if (exists(".Random.seed")) {
oldseed <- get(".Random.seed", .GlobalEnv)
} else stop("don't know how to save before set.seed() or r*** call")
oldRNGkind <- RNGkind()
save("oldseed","oldRNGkind",file=savefile)
invisible(savefile)
}

restore_rng <- function(savefile) {
load(savefile)
do.call("RNGkind",as.list(oldRNGkind)) ## must be first!
assign(".Random.seed", oldseed, .GlobalEnv)
}

Try it out:

set.seed(101)
RNGstore <- save_rng() ## save file name
runif(1)
## [1] 0.3721984
runif(10)
## [1] 0.04382482 0.70968402 0.65769040 0.24985572 0.30005483 0.58486663
## [7] 0.33346714 0.62201196 0.54582855 0.87979573
restore_rng(RNGstore)
runif(1)
## [1] 0.3721984

See also: http://www.cookbook-r.com/Numbers/Saving_the_state_of_the_random_number_generator/

How do I get the seed from a Random in Java?

A Random is intended to be random. Usually you want two Random to produce different numbers rather than to produce the same numbers.

You can copy a Random using serialisation/de-serialisation and get the "seed" field using reflection. (But I doubt you should be doing either)

Unless the sequence is critical to you, you can take the view that the clone of a Random is itself or any new Random()

How do you get the current seed of Random in C#?

Not sure on getting the seed, but you could save the value you give to the Random object. Remember, there are two constructors. The second is Random(Int32), so if you set the seed yourself (an easy enough value is Environment.TickCount), you could store that value somewhere before you pass it to the constructor. If you haven't read it yet, check out the MSDN documentation at https://docs.microsoft.com/en-us/dotnet/api/system.random.

Can I get.seed() somehow?

If you didn't keep the seed, there's no general way to "roll back" the random number generator to a previous state after you've observed a random draw. Going forward, what you may want to do is save the value of .Random.seed along with the results of your computations. Something like this.

x <- .Random.seed
result <- <your code goes here>
attr(result, "seed") <- x

Then you can reset the PRNG as follows; result2 should be the same as result.

.Random.seed <- attr(result, "seed")
result2 <- <your code goes here>


Related Topics



Leave a reply



Submit