Package org.uncommons.maths.random
Class Probability
- java.lang.Object
-
- java.lang.Number
-
- org.uncommons.maths.random.Probability
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<Probability>
public final class Probability extends java.lang.Number implements java.lang.Comparable<Probability>
Immutable value type for probabilities. Forces numeric probabilities to be within the range 0..1 inclusive and provides useful utility methods for working with probabilities (such as generating an event with a given probability).- Since:
- 1.2
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ProbabilityEVENSConvenient constant representing a probability of 0.5 (used to model an event that has a 50/50 chance of occurring).static ProbabilityONEConvenient constant representing a probability of one.private doubleprobabilitystatic ProbabilityZEROConvenient constant representing a probability of zero.
-
Constructor Summary
Constructors Constructor Description Probability(double probability)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Probability other)Compares this value with the specified object for order.doubledoubleValue()Returns the probability value as a double.booleanequals(java.lang.Object other)Determines whether this probability value is equal to some other object.floatfloatValue()Returns the probability value as a float.ProbabilitygetComplement()The complement of a probability p is 1 - p.inthashCode()Over-ridden to be consistent withequals(Object).intintValue()Converting a fractional probability into an integer is not meaningful since all useful information is discarded.longlongValue()Converting a fractional probability into an integer is not meaningful since all useful information is discarded.booleannextEvent(java.util.Random rng)Generates an event according the probability value p.java.lang.StringtoString()Formats the probability as a String.
-
-
-
Field Detail
-
ZERO
public static final Probability ZERO
Convenient constant representing a probability of zero. If an event has a probability of zero it will never happen (it is an impossibility).
-
EVENS
public static final Probability EVENS
Convenient constant representing a probability of 0.5 (used to model an event that has a 50/50 chance of occurring).
-
ONE
public static final Probability ONE
Convenient constant representing a probability of one. An event with a probability of one is a certainty.
-
probability
private final double probability
-
-
Method Detail
-
nextEvent
public boolean nextEvent(java.util.Random rng)
Generates an event according the probability value p.- Parameters:
rng- A source of randomness for generating events.- Returns:
- True with a probability of p, false with a probability of 1 - p.
-
getComplement
public Probability getComplement()
The complement of a probability p is 1 - p. If p = 0.75, then the complement is 0.25.- Returns:
- The complement of this probability.
-
intValue
public int intValue()
Converting a fractional probability into an integer is not meaningful since all useful information is discarded. For this reason, this method is over-ridden to thrown anArithmeticException, except when the probability is exactly zero or one.- Specified by:
intValuein classjava.lang.Number- Returns:
- An integer probability.
- Throws:
java.lang.ArithmeticException- Unless the probability is exactly zero or one.
-
longValue
public long longValue()
Converting a fractional probability into an integer is not meaningful since all useful information is discarded. For this reason, this method is over-ridden to thrown anArithmeticException, except when the probability is exactly zero or one.- Specified by:
longValuein classjava.lang.Number- Returns:
- An integer probability.
- Throws:
java.lang.ArithmeticException- Unless the probability is exactly zero or one.
-
floatValue
public float floatValue()
Returns the probability value as a float.- Specified by:
floatValuein classjava.lang.Number- Returns:
- A real number between 0 and 1 inclusive.
-
doubleValue
public double doubleValue()
Returns the probability value as a double.- Specified by:
doubleValuein classjava.lang.Number- Returns:
- A real number between 0 and 1 inclusive.
-
equals
public boolean equals(java.lang.Object other)
Determines whether this probability value is equal to some other object. To be considered equal the other object must also be a Probability object with an indentical probability value.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- The object to compare against.- Returns:
- True if the objects are equal, false otherwise.
-
hashCode
public int hashCode()
Over-ridden to be consistent withequals(Object).- Overrides:
hashCodein classjava.lang.Object- Returns:
- The hash code value.
-
compareTo
public int compareTo(Probability other)
Compares this value with the specified object for order. Returns a negative integer, zero, or a positive integer as this value is less than, equal to, or greater than the specified value.- Specified by:
compareToin interfacejava.lang.Comparable<Probability>- Parameters:
other- Another Probability value.- Returns:
- A negative integer, zero, or a positive integer as this value is less than, equal to, or greater than the specified value.
-
toString
public java.lang.String toString()
Formats the probability as a String. This is simply the string representation of the double value encapsulated by this probability object.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of the probability value (a number between 0 and 1 inclusive).
-
-