Reverse work out the mean using random number seed
Asked Answered
D

2

5

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?

Decker answered 7/7, 2024 at 15:46 Comment(0)
R
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.

Rucker answered 7/7, 2024 at 16:22 Comment(3)
The problem is, this won't generalize for larger sds.Wild
Why won't it generalize?? x <- 5; set.seed(101); y <- rnorm(1, x, 1000); set.seed(101); y - rnorm(1, sd = 1000)Performance
@BenBolker Absolutely, the key is to set the seed correctly, and this method works regardless of whether the mean is an integer or not.Wild
W
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
Wild answered 7/7, 2024 at 16:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.