I am trying to generate random data with R's runif
and norm
functions, where these functions are supposed to add some "noise" to the generated data. However, they are producing the same random number when I apply them as part of a vector addition operation.
Below is an example where x
is an independent variable. I would to generate y
and z
, which are dependent output variables with a little "noise" added to them by the random number generators. The noise is supposed to change randomly with every new value of x
.
x <- seq(1:10)
y <- x + runif(1)
z <- x + rnorm(1,mean=2, sd=0.5)
df <- data.frame(x, y, z)
df
# x y z
# 1 1 1.364842 3.10679
# 2 2 2.364842 4.10679
# 3 3 3.364842 5.10679
# 4 4 4.364842 6.10679
# 5 5 5.364842 7.10679
# 6 6 6.364842 8.10679
# 7 7 7.364842 9.10679
# 8 8 8.364842 10.10679
# 9 9 9.364842 11.10679
# 10 10 10.364842 12.10679
As seen above, each random number generator is creating identical values. Why is this happening? How can I fix the problem so that they generate different random numbers every time they are added to the x
variable in the vector addition operation?
Here is proof that the two functions are working correctly. I did not set any seed value.
runif(1)
# [1] 0.05202364
runif(1)
# [1] 0.02315487
rnorm(1,mean=2, sd=0.5)
# [1] 1.714753
rnorm(1,mean=2, sd=0.5)
# [1] 1.369988
Aucun commentaire:
Enregistrer un commentaire