C code for simulating Signature Method to determine the value of Pi
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<math.h> int main(void){ float x, y, p=0, n=10000; int i, r=50; srand(time(NULL)); for(i=1;i<=n;i++){ x=r-rand()%(2*r+1); y=r-rand()%(2*r+1); if(x*x+y*y-r*r<=0) p++; } printf("%f", 4*p/n); return 0; }
This code can be run here. It simulates the approximate value of pi with 10000 signatures (note line 6). You can change the input number and see what happens to the approximation.
This C code is cited in the following work(s) by Saumitra Chakravarty:
- Praner Majhe Gonit Baje: Jyamitir Jonyo Bhalobasa (BOOK) > Chapter 9 > Appendix 1
- Music of Pi Resonates Within Probability (ARTICLE)
- Goniter Sathe Bosobas (BOOK) > Chapter 2