Background
Assume that we have a game where monsters and weapons are generated “randomly”.
A weapon could be defined like this:
- type: axe (1.2) / sword (1.4) / spear (1) / club (0.7)
- range: 1 – 3
- magic: ice (10) / fire (10) / none (1)
- state: sharp (2) / normal (1) / dull (0.5)
Some of these values could have dependencies (e.g. type and range).
Using a random generator we could get for example a dull Axe of Fire (range 2). Then we could have a formula that calculates the level of this weapon. Example formula:
level = floor(type * range * magic * state)
The dull Axe of Fire (range 2) would then have level floor(1.2 * 2 * 10 * 0.5) = 12.
Simple, but nice!
We could also have a world where the player goes deeper and deeper down into the dangerous caves, starting at cave level 1.
The question
Let’s say the player is at cave level 12. (S)he has entered a room where we want to place a weapon in a chest.
What would be a good algorithm to generate a level 12 weapon? (or any other level of course)
I.e. we have the level and want to use a level calculation (our simple one, or another more sophisticated) backwards to generate good values for the different elements of the weapon.
Minor or major changes to the definition of a weapon can be made to facilitate a better solution.
EDIT:
It seems like High Performance Mark‘s solution is the best where there are a reasonably limited set of combinations. If not, or if we have continuous values and an “unlimited” set of combinations, one of the other answers might be better suited. I say “might” since maybe a lookup table could be used even in a continuous version for a subset of the values to pick a good starting point and then one could add a small random delta to fill in.
Example: range is continuous between 10 and 200. The lookup table contains values (10, 20, 30, …, 190, 200). The algorithm picks 110 for range and then adds random(-5.0, 5.0) to it for a final value of, say, 108.72. Then the final level of the weapon would be close enough to the desired value to do. How does that sound?
If you have, say, 5 choices each of type, range, magic and state then you have 5^4 choices of weapon. This is quite a small number and you could easily store them all in a lookup table, indexed on the level of weapons. When you want a random level 12 weapon, you choose uniformly from the available level 12 weapons.