Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 could just randomly generate items till I got one in the range. Obviously, this is flawed because it could take a very long time
  • Randomly choose a thing to add to the item (that is still available), keep a running weight and stop when I hit the right range or run out of available slots. I think this would bias the items toward high pluses and large numbers of prefix2 parts, because a low weight for prefix and suffix would leave a lot of weight left to fill with prefix2 items. Also it would seriously lower the chance of getting items the don't have all of the component types.
  • Some sort of heuristic that would set an appropriate lower limit on weight to prevent the above bias from occuring. This would be good because higher weight items should not have a lot of lower weight compoents.

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

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

In reply to Algorithm Help by dreadpiratepeter

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Happy-the-monk]: Good morning monks!

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (5)
    As of 2018-04-23 05:58 GMT
    Find Nodes?
      Voting Booth?