in reply to Algorithm Help
You might want to edit your post to clarify that weight does not affect the probability of a given selection.
Two ideas
You could pre-calculate (perhaps write to data files if there are many) all possible items and their strength/weight values. If your list of possible weight values is dense (few skipped values) put an array of items at each weight at a coresponding index in a master array. So:
my @items = ( # 0 [ 'sword', 'mace', 'turtle' ], # 1 [ '+1 sword', '+1 mace', '+1 turtle' ], # 2 [ 'frost sword', 'frost mace', 'frost turtle', 'flame sword', 'flame mace', 'flame turtle' ], ... );
If the set of weights is sparse, you might use a hash instead.
Then if you want to select a random item of weight 3-5, you can do:
my @in_range = map @{$items[$_]}, 3..5; my $item = $in_range[ int rand( @in_range ) ];
Another way, would be:
TGI says moo
|
|---|