Java Random Int randomPointsFromFigureEight(int n)

Here you can find the source of randomPointsFromFigureEight(int n)

Description

random Points From Figure Eight

License

Open Source License

Declaration

public static double[][] randomPointsFromFigureEight(int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double[][] randomPointsFromFigureEight(int n) {
        if (n <= 0) {
            throw new IllegalArgumentException();
        }/*from www.  j a va 2s .c  o m*/

        double[][] result = new double[n][2];

        for (int i = 0; i < n; i++) {
            /*
             * We first choose a point randomly on the unit circle
             */

            double theta = 2 * Math.PI * Math.random();

            double x = Math.cos(theta);
            double y = Math.sin(theta);

            /*
             * Now, we choose whether or not we're on the circle
             *  x^2 + (y-1)^2 = 1 or x^2 + (y+1)^2 = 1 by shifting 
             *  the y coordinate either up 1 or down 1 randomly.
             */

            y += Math.random() < 0.5 ? 1 : -1;

            result[i][0] = x;
            result[i][1] = y;
        }

        return result;
    }
}

Related

  1. randomNumberBetweenIntervals(int min, int max)
  2. randomNumberWithinRange(int low, int high)
  3. randomNumberWithinRange(int min, int max)
  4. randomPass(int n)
  5. randomPointInConvexPoly(float[] pts, int npts, float[] areas, float s, float t)
  6. randomPointsFromUnitSphere(int n)
  7. randomPointsFromUnitSquare(int n)
  8. randomQuickSort(int[] array)
  9. randomRange(int min, int max)