001/**
002 * GaussianRandomPrimitive.java
003 *
004 * Copyright (c) 2004-2012, Nicole C. Tedesco. All rights reserved.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007 * use this file except in compliance with the License. You may obtain a copy of
008 * the License at:
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015 * License for the specific language governing permissions and limitations under
016 * the License.
017 */
018
019package net.sf.jaccumulator.functions;
020
021import java.util.Random;
022
023import net.sf.jaccumulator.scalars.RoundingStrategy;
024
025/**
026 * A {@link RandomPrimitive} that returns values that conform to a
027 * {@link Random#nextGaussian() Gaussian} distribution
028 *
029 * @since JAccumulator 4.0
030 * @author Nicole Tedesco (<a
031 *         href="mailto:nicole@tedesco.name">nicole@tedesco.name</a>)
032 */
033public class GaussianRandomPrimitive
034    extends
035        RandomPrimitive
036{
037    private static final long serialVersionUID = -3262753494946626081L;
038
039    public static final GaussianRandomPrimitive INSTANCE = new GaussianRandomPrimitive();
040
041    private final RoundingStrategy _theRoundingStrategy;
042
043    public GaussianRandomPrimitive()
044    {
045        this(RoundingStrategy.HALF_UP);
046    }
047
048    public GaussianRandomPrimitive( final long aSeed )
049    {
050        this(aSeed, RoundingStrategy.HALF_UP);
051    }
052
053    public GaussianRandomPrimitive(
054        final long aSeed,
055        final RoundingStrategy aRoundingStrategy )
056    {
057        super(aSeed);
058        this._theRoundingStrategy = aRoundingStrategy;
059    }
060
061    public GaussianRandomPrimitive( final Random aRandom )
062    {
063        this(aRandom, RoundingStrategy.HALF_UP);
064    }
065
066    public GaussianRandomPrimitive(
067        final Random aRandom,
068        final RoundingStrategy aRoundingStrategy )
069    {
070        super(aRandom);
071        this._theRoundingStrategy = aRoundingStrategy;
072    }
073
074    public GaussianRandomPrimitive( final RoundingStrategy aRoundingStrategy )
075    {
076        super();
077        this._theRoundingStrategy = aRoundingStrategy;
078    }
079
080    @Override
081    public final boolean booleanValue() {
082        return this._theRoundingStrategy.booleanValue(this.doubleValue());
083    }
084
085    @Override
086    public final byte byteValue() {
087        return this._theRoundingStrategy.byteValue(this.doubleValue());
088    }
089
090    @Override
091    public final char charValue() {
092        return this._theRoundingStrategy.charValue(this.doubleValue());
093    }
094
095    @Override
096    public final double doubleValue() {
097        return this.nextGaussian();
098    }
099
100    @Override
101    public final float floatValue() {
102        return (float)this.doubleValue();
103    }
104
105    @Override
106    public final int intValue() {
107        return this._theRoundingStrategy.intValue(this.doubleValue());
108    }
109
110    @Override
111    public final long longValue() {
112        return this._theRoundingStrategy.longValue(this.doubleValue());
113    }
114
115    @Override
116    public final short shortValue() {
117        return this._theRoundingStrategy.shortValue(this.doubleValue());
118    }
119}