Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6732663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:40:38+00:00 2026-05-26T10:40:38+00:00

I’m programming a Monte-Carlo Simulation that should give the user quite some flexibility. Consequently,

  • 0

I’m programming a Monte-Carlo Simulation that should give the user quite some flexibility. Consequently, I want the user to be able to specify
the concrete probability distribution of the random numbers before the simulation is run. However, at this time it is not known to the user
how many random numbers are needed.

My idea is now to get a call-object from user that creates ONE random number and then create as many of those random numbers as needed internally. However,
except for a loop I can’t get any other solution to work, but have the feeling that this is because of me missing something. So basically, I have
two questions:

1) Is the idea with the call-object a good one? I’m still working on the project so I could still change the set-up, but I need a very intuitive
solution for the user.

2) If it is a good idea, is there are more elegant way to expand the random number to a vector of size nrMCS?

Let’s make an example:

#That's what I would get from the user with my current set-up:
rnd_call <- call("rnorm", 1, mean=0.1, sd=0.01)
#To create nrMCS random numbers, that's my best shot so far:
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
...
#Question: Is there are more elegant way?
#I tried the following, but it fails for certain reasons
rep(eval(rnd_call), nrMCS) #DOES NOT WORK: Repeats ONE random number
[1] 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 0.1105464 
...
eval(rep(rnd_call, nrMCS)) #DOES NOT WORK
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:40:39+00:00Added an answer on May 26, 2026 at 10:40 am

    I think a more idiomatic way to do this would be to take an r* function and a list of arguments. Whenever you can avoid calling eval, you should. Something like this:

    rnd_fun <- rnorm
    rnd_args <- list(mean=0.1,sd=0.01)
    nrMCS <- 100
    rnd_vec <- do.call(rnd_fun,c(list(n=nrMCS),rnd_args))
    

    (this relies on the convention in R that the first argument to an r* (random deviate generator) function is always n, the number of deviates required …)

    Furthermore, calling rnd_fun once with n=nrMCS is generally much more efficient than calling it nrMCS times …

    library(rbenchmark)
    nrMCS <- 10000
    benchmark(single_call=do.call(rnd_fun,c(list(n=nrMCS),rnd_args)),
               mult_call=replicate(nrMCS,do.call(rnd_fun,c(list(n=1),rnd_args))))
             test replications elapsed relative user.self sys.self 
    2   mult_call          100  11.135 91.27049    11.084    0.004 
    1 single_call          100   0.122  1.00000     0.080    0.036
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.