 | Hat abstract data type Imagine throwing a bunch of names into a hat, and then drawing one out at random.
// make a hat Hat<String> myHat = new Hat<String>();
// put some names in it myHat.Put("Alice"); myHat.Put("Bob"); myHat.Put("Clarise"); |
// draw a dozen names
for( int i=0; i<12; i++){
Console.Write( " " + myHat.Get() );
}Pretty sweet so far, but what if I want to add Daniel and give him more than one vote?
myHat.Put("Daniel", 4); // Daniel gets four chances
Then along came a spider... What if I already have a set of stuff all neat and tidy in an array? Maybe something like so:
String names[] = { "Amelia", "Bert", "Cherry", "Doug", "Erika" }What I'd like to do is apply a probability distribution to that set without moving all that stuff around. For example, let's say a sort of bell curve like Amelia:1, Bert:3, Cherry:5, Doug:3, Erika:1.
// Making a probability distribution
Hat<int> myRandomIndexes = new Hat<int>();
myRandomIndexes.Put(0, 1);
myRandomIndexes.Put(1, 3);
myRandomIndexes.Put(2, 5);
myRandomIndexes.Put(3, 3);
myRandomIndexes.Put(4, 1);
// rolling the curvy dice against my pre-existing name set
Console.Writeline( names[ myRandomIndexes.Get() ] );