Ludeon Forums

Ludeon Forums

  • February 02, 2023, 09:24:21 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Hey Rocky, watch me pull a rabbit out of my hat  (Read 1263 times)

AngleWyrm

  • Planetologist
  • ****
  • Posts: 1154
  • Refugee
    • View Profile
Hey Rocky, watch me pull a rabbit out of my hat
« on: June 27, 2017, 02:17:20 AM »

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() ] );
« Last Edit: July 08, 2017, 09:47:53 PM by AngleWyrm »
Logged
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

AngleWyrm

  • Planetologist
  • ****
  • Posts: 1154
  • Refugee
    • View Profile
Re: Hey Rocky, watch me pull a rabbit out of my hat
« Reply #1 on: June 27, 2017, 06:35:40 PM »

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() );
}

Code: [Select]
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]
« Last Edit: June 27, 2017, 07:34:31 PM by AngleWyrm »
Logged
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh