Question

Reverse work out the mean using random number seed

In R language version 4.4.1 (the version should not matter, just for the sake of discussion), we write the code :

set.seed(1234)

x <- 5
y <- rnorm(1, mean = x, sd = 0.1)

We will be able to print the number y. Suppose that there is a second person, he knows the number y, that we are using seed 1234 and that he knows the number is generated using this code. The only thing he does not know is the number x. Can he work out that x=5?

 4  87  4
1 Jan 1970

Solution

 5

It looks like the source code for rnorm() roughly defines it as:

rnorm(1, mean, sd) = mean + sd * norm_rand()

or using your variable names y = x + 0.1*norm_rand()
So to reverse it, I'd think the following would work:

set.seed(1234)
x <- y - 0.1*norm_rand()

If norm_rand() isn't an exposed function, I think you could replace it with rnorm(1,0,1)

I'm not an 'R' coder, and haven't tried this myself, but it looks straightforward.

2024-07-07
Rich K

Solution

 1

Calculating how much a normal distribution with mean 0 and same sd is shifted from y.

> x <- 5.03
> set.seed(1234)
> sd. <- runif(1,  1, 1e6)  ## demonstrating independence from SD
> set.seed(1234)
> y <- rnorm(1, mean=x, sd=sd.)
> 
> f <- \(y, sd) {y - rnorm(1L, 0, sd.)}
> set.seed(1234)
> f(y, sd.)
[1] 5.03
2024-07-07
jay.sf