I am programming a Monte Carlo simulation that should give the user enough flexibility. Therefore, I want the user to be able to indicate whether a specific probability distribution of random numbers is being performed before the simulation. However, at this time, the user does not know how many random numbers are needed.
Now my idea is to get a call object from a user who creates ONE random number, and then create as many of these random numbers as possible within themselves. However, apart from the cycle, I can’t get any other solution for work, but I feel that something is missing because of me. So basically, I have two questions:
1) Is the call object idea good? I am still working on a project, so I can still change the setting, but I need a very intuitive solution for the user.
2) If this is a good idea, is there a more elegant way to extend a random number to a vector of size nrMCS?
Here is an example:
rnd_call <- call("rnorm", 1, mean=0.1, sd=0.01)
nrMCS <- 100
rnd_vec <- as.numeric(nrMCS)
for (i in 1:nrMCS){rnd_vec[i] <- eval(rnd_call)}
rnd_vec
[1] 0.09695170 0.11752132 0.11548925 0.11205948 0.10657986 0.12017120 0.09518435
...
rep(eval(rnd_call), nrMCS)
[1] 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464
...
eval(rep(rnd_call, nrMCS))
Error in rnorm(1, mean = 0.1, sd = 0.01, rnorm, 1, mean = 0.1, sd = 0.01, :
formal argument "mean" matched by multiple actual arguments
source
share