Home | History | Annotate | Download | only in random
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package org.apache.commons.math.random;
     19 
     20 import org.apache.commons.math.util.FastMath;
     21 
     22 
     23 /**
     24  * Generate random vectors isotropically located on the surface of a sphere.
     25  *
     26  * @since 2.1
     27  * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 aot 2010) $
     28  */
     29 
     30 public class UnitSphereRandomVectorGenerator
     31     implements RandomVectorGenerator {
     32     /**
     33      * RNG used for generating the individual components of the vectors.
     34      */
     35     private final RandomGenerator rand;
     36     /**
     37      * Space dimension.
     38      */
     39     private final int dimension;
     40 
     41     /**
     42      * @param dimension Space dimension.
     43      * @param rand RNG for the individual components of the vectors.
     44      */
     45     public UnitSphereRandomVectorGenerator(final int dimension,
     46                                            final RandomGenerator rand) {
     47         this.dimension = dimension;
     48         this.rand = rand;
     49     }
     50     /**
     51      * Create an object that will use a default RNG ({@link MersenneTwister}),
     52      * in order to generate the individual components.
     53      *
     54      * @param dimension Space dimension.
     55      */
     56     public UnitSphereRandomVectorGenerator(final int dimension) {
     57         this(dimension, new MersenneTwister());
     58     }
     59 
     60     /** {@inheritDoc} */
     61     public double[] nextVector() {
     62 
     63         final double[] v = new double[dimension];
     64 
     65         double normSq;
     66         do {
     67             normSq = 0;
     68             for (int i = 0; i < dimension; i++) {
     69                 final double comp = 2 * rand.nextDouble() - 1;
     70                 v[i] = comp;
     71                 normSq += comp * comp;
     72             }
     73         } while (normSq > 1);
     74 
     75         final double f = 1 / FastMath.sqrt(normSq);
     76         for (int i = 0; i < dimension; i++) {
     77             v[i] *= f;
     78         }
     79 
     80         return v;
     81 
     82     }
     83 
     84 }
     85