# Monte carlo algorithms in C++

Gold Member
I would like to discuss code for hit and miss monte carlo methods, and also monte carlo with veto algorithm in C++. Since I am coding in C++ after a long time, I am messed up with syntax too. I have a specific set of problems to work with. If interested we can start working on it here.

Nguyen Son
Could you code Matlab, besides C++?

Gold Member
Could you code Matlab, besides C++?
Used it once before, but lost touch.

Hello @Chromatic_Universe

I have a specific set of problems to work with.
There are numerous members here (including myself) with experience in C++ and monte carlo methods. If you have specific problems that you are stuck on, please create a post for each specific set of issues. Also if these are homework problems, please remember to use the homework template in your post.

Gold Member
Hello @Chromatic_Universe

There are numerous members here (including myself) with experience in C++ and monte carlo methods. If you have specific problems that you are stuck on, please create a post for each specific set of issues. Also if these are homework problems, please remember to use the homework template in your post.
Will do so! Thanks !

Gold Member
We have the function sin^2/x^2, given x>=0. Now we have to generate random numbers according to the given distribution using Monte Carlo hit-and-miss method. I want to do it in C++.
@NFuller you can see the problem now.

Homework Helper
Gold Member
We have the function sin^2/x^2, given x>=0.
Is this correct? What are you taking the sin of? sin2(?)/x2
Now we have to generate random numbers according to the given distribution using Monte Carlo hit-and-miss method. I want to do it in C++.
Do you mean the Monte Carlo rejection sampling method for generating a random variable from a probability density? This example presents a problem because of the unbounded x range. That is more than a programming problem. Do you have a specific way that you want to address that problem? (You may want to consider that there will be a lot of samples rejected when x is large.)

Gold Member
Is this correct? What are you taking the sin of? sin2(?)/x2
Do you mean the Monte Carlo rejection sampling method for generating a random variable from a probability density? This example presents a problem because of the unbounded x range. That is more than a programming problem. Do you have a specific way that you want to address that problem? (You may want to consider that there will be a lot of samples rejected when x is large.)
@FactChecker It would be (sin x)2/x2. And it does not blow up at x=0 because we apply L'Hopital's rule(https://en.wikipedia.org/wiki/L'Hôpital's_rule), hence it's finite(You can also check the intensity distribution for a single slit diffraction experiment - http://hyperphysics.phy-astr.gsu.edu/hbase/phyopt/sinint.html).
A way to do it would be to take a test function g(y) which is always greater than f(x)=(sin x)2/x2(where x is a random number) for the given range and generate random "y' " and calculate values of g(y') and check with f(x). If g(y) <= f(x), we accept, otherwise we reject it. Then we repeat the procedure again.
We consider g(y) to increase efficiency of the algorithm. Otherwise, as you mentioned -
You may want to consider that there will be a lot of samples rejected when x is large.

Homework Helper
Gold Member
@FactChecker It would be (sin x)^2/x^2. And it does not blow up at x=0 because we apply L'Hopital's rule(https://en.wikipedia.org/wiki/L'Hôpital's_rule), hence it's finite(You can also check the intensity distribution for a single slit diffraction experiment - http://hyperphysics.phy-astr.gsu.edu/hbase/phyopt/sinint.html).
Yes. I forgot for a moment that the sin was squared. I corrected it a few minutes later.
A way to do it would be to take a test function g(y) which is always greater than f(x)=(sin x)2/x2(where x is a random number) for the given range and generate random "y' " and calculate values of g(y') and check with f(x). If g(y) <= f(x), we accept, otherwise we reject it. Then we repeat the procedure again.
We consider g(y) to increase efficiency of the algorithm. Otherwise, as you mentioned -
Ok. You may want to consider the problems presented by the unbounded x range and the high number of rejections for large values of x.