#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Recursive Roll Subtracting from Randomly Generated Total

edited February 2016
I'm messing around trying to create treasure tables. Using the 5E DMG as an example, a treasure hoard may contain 2d6 Gems each priced at 50 gp. They also have a table of 12 different gems, each worth 50 gp. At the moment I only know how to either pull 2d6 unique gems (using a deck) or just pick one gem from a table and say you find 2d6 of them.

Is there an established method, or example, of how to pull quantities from a deck that add up to a given total? For instance:

[2d6] = 9 Total Gems => [1d4]=3x Gem A, [1d4]=2x Gem E, [1d4]=4x Gem D.

I'd imagine you'd need to use recursion but I'm not having any luck getting it to work. Is there a better way?

Thank you!

## Comments

• I'm not sure if this is what you have in mind, but you can use hashes to get it done. Hashes are detailed here:
http://www.nbos.com/forums/viewtopic.php?f=16&t=986&sid=f864757be3ab22f532a4b3f2c3124eb7
```use: common/nbos/Hashes.ipt

set: currHash =myHash

table: start
&#91;@{2d6} Gems &gt;&gt; each AddToHash&#93;&
&#91;@&;#91;@HashCount&;#93; HashKeys &gt;&gt; each ShowResults &gt;&gt; implode&#93;

table: ShowResults
&#91;@HashGet with {\$1}&#93; {\$1}

table: AddToHash
&#91;@HashSet with {\$1}, {&#91;@HashGet with {\$1}&#93;+1}&#93;

table: Gems
diamonds
rubies
opals
emeralds
sapphires
```

Example results:
1 opals, 3 sapphires, 3 diamonds, 2 emeralds, 1 rubies
4 sapphires, 2 diamonds, 1 rubies
2 opals, 1 rubies, 1 emeralds
3 rubies, 2 opals, 1 sapphires
1 diamonds, 1 opals, 2 emeralds, 2 sapphires, 1 rubies
• Hi, this is very similar to something I am trying to do and I pasted the exact code and I get this as results:

[@10 Gems >> each AddToHash][@&;;#91;@HashCount&;;#93; HashKeys >> each ShowResults >> implode]

[@7 Gems >> each AddToHash][@&;;#91;@HashCount&;;#93; HashKeys >> each ShowResults >> implode]

[@10 Gems >> each AddToHash][@&;;#91;@HashCount&;;#93; HashKeys >> each ShowResults >> implode]

[@5 Gems >> each AddToHash][@&;;#91;@HashCount&;;#93; HashKeys >> each ShowResults >> implode]

[@6 Gems >> each AddToHash][@&;;#91;@HashCount&;;#93; HashKeys >> each ShowResults >> implode]

I noticed my install didn't put a Hashes.ipt in common/nbos/. Where do I get that file?
Thanks!

Drop image/file