|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.softsynth.jmsl.util.WeightedObjectChooser
public class WeightedObjectChooser
Holds objects and associated weights. Weights are used to govern the likelihood of an object
being chosen with next().
Every call to next() first sums the weights, and an object is chosen by comparing a randomly
chosen threshold against a running total as objects are enumerated.
For example, the following adds two objects where one is twice as likely to be chosen as the
other
WeightedObjectChooser chooser = new WeightedObjectChooser(); chooser.addWeightedObject(new Object(), 1.0); chooser.addWeightedObject(new Object(), 2.0); Object obj = chooser.next();This class was requested by Larry Polansky.
Constructor Summary | |
---|---|
WeightedObjectChooser()
|
Method Summary | |
---|---|
void |
addWeightedObject(java.lang.Object object,
double weight)
Add an Object and associate it with specified weight |
boolean |
contains(java.lang.Object object)
|
java.util.Enumeration |
elements()
|
double |
getWeight(java.lang.Object object)
|
static void |
main(java.lang.String[] args)
source: |
java.lang.Object |
next()
|
void |
remove(java.lang.Object object)
remove specified object from objects under consideration |
void |
setWeight(java.lang.Object object,
double weight)
Set weight of an Object. |
int |
size()
|
java.lang.String |
toString()
|
double |
totalWeights()
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public WeightedObjectChooser()
Method Detail |
---|
public java.lang.Object next()
public void addWeightedObject(java.lang.Object object, double weight)
public void setWeight(java.lang.Object object, double weight)
public double getWeight(java.lang.Object object)
public void remove(java.lang.Object object)
public java.lang.String toString()
toString
in class java.lang.Object
public double totalWeights()
public java.util.Enumeration elements()
public int size()
public boolean contains(java.lang.Object object)
public static void main(java.lang.String[] args)
public static void main(String args[]) { JMSLRandom.randomize(); WeightedObjectChooser chooser = new WeightedObjectChooser(); Object gaga1 = new Object(); Object gaga2 = new Object(); chooser.addWeightedObject(gaga1, 1.0); chooser.addWeightedObject(gaga2, 1.0); System.out.println(chooser.toString()); int gaga1kount = 0; int gaga2kount = 0; for (int i = 0; i < 100000; i++) { Object obj = chooser.next(); if (obj.equals(gaga1)) gaga1kount++; if (obj.equals(gaga2)) gaga2kount++; } System.out.println("gaga1 count=" + gaga1kount + ", gaga2 count=" + gaga2kount); }
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |