in a simulation program like below

Program the following simulation: Darts are thrown at random points onto a square with corners
(1, 1) and (−1, −1). If the dart lands inside the unit circle (that is, the circle with center (0, 0) and radius 1), it is a hit. Otherwise it is a miss. Run this simulation and use it to determine an approximate value for PI.

how would u calculate if the random# are inside the circle or outside ....

#include <iostream>
  #include <string>
  #include <cstdlib>
  #include <ctime>
   
  using namespace std;
   
  void rand_seed()
  {
              int seed=static_cast<int>(time(0));
              srand (seed);
  }
   
  double rand_x(int a, int b)
  {
              return a +(b-a)*rand()*(1.0/RAND_MAX);
  }
   
  double rand_y(int c, int d)
  {
              return c +(d-c)*rand()*(1.0/RAND_MAX);
  }
   
  int main()
  {
              rand_seed();
              for (int i=1; i<=5;i++)
              {
                          double x1=rand_x(-1,1);
                          double y1=rand_x(-1,1);
                          cout<<" x value "<<x1<<" y value is "<<y1<<"\n" ;
              }
              return 0;
  }

the points that are being created are inside the square but what could be written for checking if the points are inside the unit circle ...

help would be great

cheers

Check if the distance between the center of your square and the random point is less than equal to the radius. If so then the dart has hit the spot otherwise its a miss. This validation should be easy to perform using the distance formula.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.