A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation. : Random « Development Class « Java






A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.

  
// $Id: GaussianHat.java 12 2009-11-09 22:58:47Z gabe.johnson $

//package org.six11.util.data;

import java.util.Random;

/**
 * A GaussianHat is a random number generator that will give you numbers based
 * on a population mean and standard deviation. You can ask it for integers or
 * probabilities. You may also specify a floor and ceiling (so in that case it's
 * not truely Gaussian).
 */
public class GaussianHat {

  // this is necessary because if you create a few GaussianHats
  // during the same millisecond and then use them in the exact
  // manner, they will yield the same 'random' numbers if you ask
  // for the same kinds of random numbers in the same order, which
  // is absolutely no good. So we'll create a single Random that
  // will then generate better Randoms from it.
  static Random staticRand;
  static {
    staticRand = new Random(System.currentTimeMillis());
  }

  double mean;
  double sd;
  Random rand;
  double floor;
  double ceiling;

  /**
   * Creates a copy of the input GaussianHat.
   */
  public GaussianHat(GaussianHat other) {
    this(other.mean, other.sd, other.floor, other.ceiling);
  }

  public GaussianHat(double mean, double sd, double floor, double ceiling) {
    this.mean = mean;
    this.sd = sd;
    this.floor = floor;
    this.ceiling = ceiling;
    this.rand = new Random(staticRand.nextLong());
  }

  public GaussianHat(double mean, double sd) {
    this(mean, sd, 0, Double.MAX_VALUE);
  }

  public GaussianHat(int mean, double sd) {
    this((double) mean, sd);
  }

  public GaussianHat(int mean, int sd) {
    this((double) mean, (double) sd);
  }

  public GaussianHat(int mean, int sd, int floor, int ceiling) {
    this((double) mean, (double) sd, (double) floor, (double) ceiling);
  }

  public GaussianHat(double mean) {
    this(mean, 1.0);
  }

  public void setMean(double mean) {
    this.mean = mean;
  }

  public void setStdDev(double sd) {
    this.sd = sd;
  }

  public int getInt() {
    // simply return getDouble() as an int.
    return (int) getDouble();
  }

  public double getDouble() {
    // based on the mean and standard deviation, pick a number from a normal
    // distribution, cast it to an integer
    double d = (rand.nextGaussian() * sd) + mean;
    if (d < floor)
      d = floor;
    if (d > ceiling)
      d = ceiling;
    return d;
  }

  public double getUniformDouble() {
    double d = rand.nextDouble();
    d = floor + (d * (ceiling - floor));
    return d;
  }

  public boolean getYesNo() {
    // get a random number from uniform distribution 0..1 and return true if
    // that number is less than or equal to the provided 'chance' parameter
    return (rand.nextDouble() <= mean);
  }

  public String toString() {
    return mean + " (" + sd + ")";
  }

  public static void main(String[] args) {
    // I have convinced myself that my randomness is OK using JMP
    GaussianHat[] intHats = new GaussianHat[] { new GaussianHat(10, 2),
        new GaussianHat(10, 4), new GaussianHat(20, 2),
        new GaussianHat(20, 4) };

    GaussianHat[] chanceHats = new GaussianHat[] {
        new GaussianHat(0.60, 0.05), new GaussianHat(0.60, 0.15),
        new GaussianHat(0.30, 0.05), new GaussianHat(0.30, 0.15) };
    for (int trial = 1; trial <= 100; trial++) {
      for (int i = 0; i < intHats.length; i++) {
        System.out.print(intHats[i].getInt() + "\t");
        if (i == (intHats.length - 1))
          System.out.println();
      }
    }
    for (int trial = 1; trial <= 100; trial++) {
      for (int i = 0; i < chanceHats.length; i++) {
        System.out.print(chanceHats[i].getYesNo() + "\t");
        if (i == (chanceHats.length - 1))
          System.out.println();
      }
    }
  }
}

   
    
  








Related examples in the same category

1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.A random.org seeded random generator.
31.Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.
32.A random choice maker
33.Memory-efficient map of keys to values with list-style random-access semantics.
34.Generates a random integer inside the lo and hi interval
35.Randomizer
36.RandomGUID generates truly random GUIDs
37.A random access text file allows a text file to be accessed randomly starting at any position.
38.A garbage-free randomised quicksort
39.A linear random method based on xor shifts
40.Mersenne Twister Random
41.Randomly permutes the elements of the specified array
42.generate string consists of random characters.
43.Atomic Pseudo Random
44.Atomic Simple Random
45.Mersenne
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice