Help with project (implementing math functions)

I'm working a project and I'm hitting some trouble along the way in the directions. I'm supposed to be implementing Monte Carlo methods in the function in order to properly run it, however the steps I am following are worded weirdly and I'm having trouble understanding. Any pointers in the right direction would be greatly appreciated. I'll leave the directions and what I have written in the code so far below. Thanks.

-CC

Directions:

Tasks:
Write a program that prompts the user to input the number of samples. Then generate that
many random points from the domain x = [0, 1], y = [0,1] and calculate and print an estimate of
Pi. Follow the pseudocode below:
declare an integer variable to count the number of samples which fall in the circle and
initialize it to 0
declare double variables x and y
prompt the user to input the number of samples
loop number of samples times
set x equal to a random number between 0 and 1
set y equal to a random number between 0 and 1
if the square root of (x * x + y * y) is less than 1, then the point we chose
randomly is within the circle, so increment the variable counting samples in the
circle
end of loop
Print the estimate of pi, which is 4 times (number of samples found inside the
circle)/(total number of samples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>

int main(int argc, const char * argv[]) {
    
    int samp = 0;
    double x = 0.0, y = 0.0;
    int count = 0;
    
    std::cout << "Please enter the number of samples: " << std::endl;
    std::cin >> samp;
    
    for (count = 0; count < 0; count++)
       double x = [0,1]
        double y = [0,1]
        
        if ((x * x + y * y) > 0)
            
    
http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <random>
#include <iostream>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0.0, 1.0);
    int samp;
    std::cin >> samp;
    for (int n = 0; n < samp; ++n) 
    {        
        std::cout << dis(gen) << ' '; //Each call to dis(gen) generates a new random double
    }
    std::cout << '\n';
}


Should get you doubles from 0.0 <= n < 1.0. You're almost there otherwise. sqrt() function comes from header <cmath>
steps I am following are worded weirdly

What in them is weird?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int main(int argc, const char * argv[]) // are these arguments needed?
{
    int samp = 0; // declare an integer variable to count the number of
        // samples which fall in the circle and initialize it to 0

    double x = 0.0, y = 0.0; // declare double variables x and y
    int count = 0;

    // prompt the user to input the number of samples
    std::cout << "Please enter the number of samples: " << std::endl;
    std::cin >> samp;
    
    for (count = 0; count < 0; count++) // loop number of samples times ???

       double x = [0,1] // set x equal to a random number between 0 and 1 ??
       double y = [0,1] // set y equal to a random number between 0 and 1 ??
        
       if ( (x * x + y * y) > 0 ) // if the square root of (x * x + y * y) is less than 1 ??
          // increment the variable counting samples ?

    // end of loop ?

    // Print the estimate of pi 

* How many times does your loop repeat?
* What statements does the loop repeat?
See http://www.cplusplus.com/doc/tutorial/control/

* What is your "variable counting samples"?


The random numbers needs a bit more than what you have.
This would be awfully close: http://www.cplusplus.com/reference/random/uniform_real_distribution/
Alas, it returns [a,b) rather than [a,b]. As such, you would never get (1,0) or (0,1).

http://www.cplusplus.com/reference/random/uniform_int_distribution/ does return [a,b] as required, but you have to map them to desired float range.

(Choice of generator and seed are important/non-trivial too, but can be added later.)
I just was unsure that the steps I was following were correct, so I guess it was more of me second guessing my code than actually misunderstanding.

Thank you for the help, guys. Much appreciated. I'll see what I can do from here.
Additional note on math:
square root of (x * x + y * y) is less than 1

The x*x >= 0 and y*y >= 0. Therefore, the (x*x + y*y) >= 0, always. That is trivial, but good to note. You don't take square root from negative value, unless you are ready for complex math.

The sqrt(x*x + y*y) is also >=0. Not negative.

When you have a<b and both are non-negative, then (a*a)<(b*b) is also true.

Let
a = sqrt(x*x + y*y)
b = 1
Both are non-negative. If a<b, then (a*a)<(b*b) and more importantly,
if (a*a)<(b*b) then a<b.

In other words, if (x * x + y * y) < (1 * 1)
then "square root of (x * x + y * y) is less than 1".
Topic archived. No new replies allowed.