home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (UnitSphereRandomVectorGeneratorTest.java)

This example Java source code file (UnitSphereRandomVectorGeneratorTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

bucket, jdkrandomgenerator, test, unitsphererandomvectorgenerator, unitsphererandomvectorgeneratortest

The UnitSphereRandomVectorGeneratorTest.java Java example source code

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.apache.commons.math3.random;

import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;

public class UnitSphereRandomVectorGeneratorTest {
     * Test the distribution of points from {@link UnitSphereRandomVectorGenerator#nextVector()}
     * in two dimensions.
    public void test2DDistribution() {

        RandomGenerator rg = new JDKRandomGenerator();
        UnitSphereRandomVectorGenerator generator = new UnitSphereRandomVectorGenerator(2, rg);

        // In 2D, angles with a given vector should be uniformly distributed
        int[] angleBuckets = new int[100];
        int steps = 1000000;
        for (int i = 0; i < steps; ++i) {
            final double[] v = generator.nextVector();
            Assert.assertEquals(2, v.length);
            Assert.assertEquals(1, length(v), 1e-10);
            // Compute angle formed with vector (1,0)
            // Cosine of angle is their dot product, because both are unit length
            // Dot product here is just the first element of the vector by construction
            final double angle = FastMath.acos(v[0]);
            final int bucket = (int) (angleBuckets.length * (angle / FastMath.PI));

        // Simplistic test for roughly even distribution
        final int expectedBucketSize = steps / angleBuckets.length;
        for (int bucket : angleBuckets) {
            Assert.assertTrue("Bucket count " + bucket + " vs expected " + expectedBucketSize,
                              FastMath.abs(expectedBucketSize - bucket) < 350);

     * @return length (L2 norm) of given vector
    private static double length(double[] vector) {
        double total = 0;
        for (double d : vector) {
            total += d * d;
        return FastMath.sqrt(total);

Other Java examples (source code examples)

Here is a short list of links related to this Java UnitSphereRandomVectorGeneratorTest.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.