001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math.random;
019    
020    
021    /**
022     * Generate random vectors isotropically located on the surface of a sphere.
023     *
024     * @since 2.1
025     * @version $Revision: 925812 $ $Date: 2010-03-21 11:49:31 -0400 (Sun, 21 Mar 2010) $
026     */
027    
028    public class UnitSphereRandomVectorGenerator
029        implements RandomVectorGenerator {
030        /**
031         * RNG used for generating the individual components of the vectors.
032         */
033        private final RandomGenerator rand;
034        /**
035         * Space dimension.
036         */
037        private final int dimension;
038    
039        /**
040         * @param dimension Space dimension.
041         * @param rand RNG for the individual components of the vectors.
042         */
043        public UnitSphereRandomVectorGenerator(final int dimension,
044                                               final RandomGenerator rand) {
045            this.dimension = dimension;
046            this.rand = rand;
047        }
048        /**
049         * Create an object that will use a default RNG ({@link MersenneTwister}),
050         * in order to generate the individual components.
051         *
052         * @param dimension Space dimension.
053         */
054        public UnitSphereRandomVectorGenerator(final int dimension) {
055            this(dimension, new MersenneTwister());
056        }
057    
058        /** {@inheritDoc} */
059        public double[] nextVector() {
060    
061            final double[] v = new double[dimension];
062    
063            double normSq;
064            do {
065                normSq = 0;
066                for (int i = 0; i < dimension; i++) {
067                    final double comp = 2 * rand.nextDouble() - 1;
068                    v[i] = comp;
069                    normSq += comp * comp;
070                }
071            } while (normSq > 1);
072    
073            final double f = 1 / Math.sqrt(normSq);
074            for (int i = 0; i < dimension; i++) {
075                v[i] *= f;
076            }
077    
078            return v;
079    
080        }
081    
082    }