0

I am determining the value of PI based on ernesto cesaros therom. I am using the default random method in java. I set the seed value as an input which will determine how many pairs of random numbers it will generate. I am always getting a value around 2.4494

    import java.util.Random;
import java.util.Scanner;

public class Projectone {
public static int count, sum = 0;
public static int a, b, gd;

public static void main(String[] args) {

    // Enter seed number
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter seed value: ");
    int seed = kb.nextInt();
    // Generate pairs of random numbers based on the seed number
    Random rand = new Random();
    for (int i = 0; i <= seed; i++) {
        count++;
        int a = rand.nextInt();
        int b = rand.nextInt();
        // Eliminating all negative numbers
        if (a < 0) {
            a *= -1;
        }
        if (b < 0) {
            b *= -1;
        }
        // Entering random numbers into gcd
        gd = gcd(a, b);
        System.out.println("a = " + a + " b= " + b);
        // breaks loop if gcd is =1 and adds the gcd
        if (gd == 1)
            break;

        for (int j = 0; j <= seed; j++) {
            sum += gd;

        }

    }
    System.out.println("this is the count " + count);
    if (sum == 0) {
        sum = 1;
    }
    System.out.println("The sumation of the gcd's = " + sum);
    //pluging in the values to the ceseros formula
    float pi=(float) Math.sqrt(6.f*count/sum);
    System.out.println("the ans is: "+pi);
}
public static int gcd(int a, int b) {
    while (a != 0 && b != 0) {
        int c = b;
        b = a % b;
        a = c;
    }
    return a + b;
     }
  }

Also I was wondering how to generate true random numbers

  • When you mention `seed` I automatically thought of https://docs.oracle.com/javase/7/docs/api/java/util/Random.html#Random(long) – Scary Wombat Feb 07 '17 at 00:46
  • You will find the algorithm for the calculation here http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/ ... it is in Python, but its very clear – Kh.Taheri Feb 07 '17 at 00:49
  • @Kh.Taheri that's a completely different algorithm... – fvu Feb 07 '17 at 01:35
  • where is the source (the algorithm) that you are following? – gpasch Feb 07 '17 at 02:23
  • for the theorem I am calculating pi =(sqrt(6*n))/p n= number of pairs of x and y. i this case it is equal to the seed value p=gcd sum – Raghu Varma Manthena Feb 07 '17 at 02:42

2 Answers2

1

You should actually code what the theorem states, which is that

the probability that two random numbers are coprimes is 6 / pi^2

Which is 0.60792710185.

or stated otherwise

pi = sqrt(6 / found_probability)

and thus

pi = sqrt(6 * tries / found_coprimes)

Obviously, you'll need to repeat the test a serious number of times to obtain a reasonable approximation of pi.

Now, in your code:

  • you stop iterating the first time you encounter a gcd of 1 (ie, a coprime). Right there and then it's game over.
  • and then you start adding gcd, for a reason that's completely unclear to me. the obtained gcd values are completely irrelevant for the result.

Consider this code:

private static int gcd(int a, int b) {
    while (a != 0 && b != 0) {
        int c = b;
        b = a % b;
        a = c;
    }
    return a + b;
}

public static void main(String[] args) {
    Random random = new Random();

    int iv = 1000000;
    int coprime = 0;

    for (int i = 0; i < iv; i++) {
        int int1 = Math.abs(random.nextInt());
        int int2 = Math.abs(random.nextInt());
        if (gcd(int1, int2) == 1) {
            coprime++;
        }
    }
    System.out.println(Math.sqrt(6.0 * iv / coprime));
}

which gives results like

3.1425778292704583


Regarding your second question, the standard "random" number generators are actually pseudo random generators. True random numbers are very hard to get, read True random generation in Java

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79
-1

The probability that your numbers are relatively prime is

P=number of pairs that are relatively prime/total number of pairs

Based on this you can reformulate your algorithm:

pi=Math.sqrt(6/P)

Plus your loops should not reach i<=upperLimit if you are starting from 0; i < upperLimit

gpasch
  • 2,672
  • 3
  • 10
  • 12