Loop + Random numbers = help needed for a beginner!

This is all the info given to me in the directions of the book:

How this program works:

If a circle of radius r is inscribed inside a square with side length 2r, then the area of the circle will be pi*r^2and the area of the square will be (2r)^2. So the ratio of the area of the circle to the area of the square will be pi/4.

This means that, if you pick N points at random inside the square, approximately N*pi/4 of those points should fall inside the circle.

This program picks points at random inside the square. It then checks to see if the point is inside the circle (it knows it's inside the circle if (x – xc)^2 + (y - yc)^2 < r^2, where x and y are the coordinates of the point and r is the radius of the circle and xc and yc are the coordinates of the center of the circle. The program keeps track of how many points it's picked so far (N) and how many of those points fall inside the circle (M).

Although the Monte Carlo Method is often useful for solving problems in physics and mathematics which cannot be solved by analytical means, it is a rather slow method of calculating pi. To calculate each significant digit there will have to be about 10 times as many trials as to calculate the preceding significant digit. Calculate pi correct to four three decimal places. Run the loop 5,000 times to start. After seeing the result, determine if you need to increase the number of times the loop runs.

hints: make the center of the circle (1,1) and the radius 1. Then what is the range of your random numbers for your generated x and y coordinates? What type of random number are you generating (int or double)? What type of loop will be best to use?

I am having a lot of trouble setting this one up. This is my code so far:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()

double radius, xUser, yUser, num;

srand(time(0));

num = (double) (rand( )%3)/RAND_MAX;


pow(num-xUser, 2) + pow (num-yUser, 2) < pow(radius, 2);


return 0;

}



How and where would I set up my loop and how do i put that together with my random numbers? I'm having a lot of trouble doing this one..
You have some nice equations, but you don't have anything that drives the inputs;

You need to put your loop around these questions. If you want to calculate 5000 times then do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int main()
{
	double radius, xUser, yUser, num;
	srand(time(0));
	for (int i = 0; i < 5000; ++i)
	{
		num = (double) (rand( )%3)/RAND_MAX;
		std::cout << pow(num-xUser, 2) + pow (num-yUser, 2) < pow(radius, 2);
	}
	return 0;
}
Topic archived. No new replies allowed.