How would I go about finding the centre of mass of a torus that obeys the following conditions in C (using Monte Carlo integration)?

So far this is the code I have. I'm using a specific rng that generates numbers between 0 and 1. It provides a basic MC integration method:

#include <stdio.h>
#include <math.h>

double random double(){
 static const int c a = 16807
 static const int c = 0;
 static const long long m = 2147483647;
 static long long seed = 1;
 seed = (a * seed + c) % m;
 return ((double) seed) / m;

int main(){
 int i;
 double x, y, z, N, integral, sum=0;
 double F(double x, double y){
  return sqrt(x*x + y*y);
 for (i = 0; i < N; i++){
  x = random_double(i);
  y = random_double(i);
  F(x, y);
  if(F(x, y) <= 1)
 integral = sum/N;
 printf("The integral is %e\n", integral);
 printf("Pi is %e\n", integral*4);

This code is just to obtain a value for pi, but I'd like to expand upon it. I'm having trouble applying it to the torus equation to find its centre of mass. Since by symmetry we know that the z coordinate for the CoM is zero, I tried scaling it down to a 2D problem.

double sumT=0, integralT, area;
for (i = 1; i < N; i++){
 x = 4*random_double(i);
 y = 7*random_double(i) - 3;
 if (F(x, y) <= 4){
 if(F(x, y) <= 2){
 if(x <= 1){
area = 7*4*sumT;
integralT = area/N;
printf("Integral is %e\n", integralT);

  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Mar 22 at 17:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.