I'm working on a TypeScript library for working with weighted random tables of the sort used by the sadly defunct Abulafia / randomgenerator.org.
The canonical example looks like this:
;PreservationSubstance
0,this is a comment and should not appear in results
5,alcohol
2,salt packing
1,formaldehyde
When queried, a "PreservationSubstance" should be alcohol with probability 5/7, salt packing 2/7, and formaldehyde 1/7.
Currently, when parsing this raw table data I transform it into an array with duplicate entries as needed:
["alcohol","alcohol","alcohol","alcohol","alcohol","salt packing","salt packing","formaldehyde"]
This is my "simplest thing that could possibly work" attempt. It allows getting the result in constant time, but it seems potentially wasteful from a memory perspective. It's working OK with thousands of entries, but I'm concerned about continued scaling.
Is there another data structure suited to this sort of problem that I should consider?