Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get.seed() somehow?

Tags:

r

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 code (interactively / at the console) containing a function that randomises some sample of the input data (the function is part of the kohonen package). After playing with it for some time to see the variety of output (it was an 'unstable' problem), I noticed one result that was pretty interesting. I of course had not used set.seed(), but wondered if I could get the seed after running the code to reproduce the result?

In ?set.seed I see

.Random.seed saves the seed set for the uniform random-number generator

But I don't know how that helps.

like image 715
a different ben Avatar asked Oct 27 '13 03:10

a different ben


People also ask

What is seed () in R?

seed() function sets the starting number used to generate a sequence of random numbers – it ensures that you get the same result if you start with that same seed each time you run the same process. For example, if I use the sample() function immediately after setting a seed, I will always get the same sample.

How do you get a Java seed?

Java doesn't provide a standard way of retrieving the seed from a Random object. If you really need that number, you may work around it: serialize your Random object, serialize another Random object (with a different seed), find the 8 bytes where these two strings differ, and retrieve the seed value from those 8 bytes.

How does seed work in random?

What is a Random Seed? A random seed is a starting point in generating random numbers. A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001).


1 Answers

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> 
like image 176
Hong Ooi Avatar answered Oct 02 '22 16:10

Hong Ooi