I am having trouble on one of my problems for my C++ class. The trouble is that I don't really understand how to use random numbers and such.
The problem is to use the Monte Carlo method to estimate the integral of fx=2x^2+3x-2.
1)Pass the function the total numbers of darts to be thrown (q).
2)For each dart thrown generate two random numbers, x from a to b, and y from 0 to m, and consider the point (x,y) as being where the darts hit.
3)Determine if the point lies under the curve or not. If it does, increment p.
4)After all darts have been thrown, compute the area using the equation: p/q=(area under curve)/(area of rectangle)
This looks like it should be working OK...I think there might be an error in determining the answer in your monte() function by using p / q alone (your description says p/q = integral area/rectangle area, so there's one more piece of algebra to be done), but codewise, this should be fine.