http://www.perlmonks.org?node_id=818015

dreadpiratepeter has asked for the wisdom of the Perl Monks concerning the following question:

I could use some help here as I am a bear of little brain. I am writing some rpg stuff and I am working on a method that generates random items a la Diablo. Things like a "Flaming silver +3 longsword of Slay Undead".

The items are of a form:

<prefix1>? <prefix2>{0,3) <plus> <weapon_type> <suffix>?

For purposes of discussion assume my list of each type of component looks like:

@prefix1 = ( [flaming => 2], [frost => 2], [acid => 4], ... );
where the second parameter is a weight. The weapon type is just a simple unweighted choice and for argument the plus runs from 0-5 with the value also being the weight.

So, for example:

  +2 mace  = weight 2
  Flaming longsword = weight 2
  Acid +3 dagger = weight 7

Here is the challenging part, that I haven't solved. Given a weight range, generate an item within that range.

Note that you can add only 1 prefix and 1 suffix to an item, but you can add up to 3 prefix2.

I had a few thoughts as to an algorithm, but they all have problems:

I hope it is clear what I am trying to do. I am not looking for code, just some ideas for an elegant solution.



-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."