(https://s6.postimg.org/5szqliij5/rocky.png) | 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() ] );
(https://s-media-cache-ak0.pinimg.com/736x/50/10/61/501061a4f41593b341030c2adf361512--cyberpunk-art-sci-fi-art.jpg) | Update What if I want to draw items out of a hat like cards from a deck, so that each one can only happen once?
// make a hat with some stuff in it Hat<String> deckOfCards = new Hat<String>("Alpha", "Bravo", "Charlie", "Delta", "Foxtrot");
while( deckOfCards.HasContents() ){ Console.Write( " " + deckOfCards.Pull() ); } |
public interface IHat<Type>
{
void Put(Type item, uint chances);
Type Get();
Type Pull();
bool HasContents();
void Print();
void PrintNode(uint index);
}
[attachment deleted by admin due to age]