Today we're building together a weighted random generator. You may also know it as a random loot table generator, or gacha system.
In short, what it does is extract a random object considering its weight. An object with a higher weight is more likely to be picked up than an item with a smaller weight (or call it chance).
As a side quest, we're going to keep code coverage at 100% ✅
If you're on node, you can install it with
npm i wrand and you can use it like so:
const picker = new RandomPicker(items); const result = picker.pick(); const results = picker.pickMany(3);
As seen in the preview, we're going to build a generator (with a class) that takes an array of objects in the constructor and will spit out randomly picked items with two methods:
As we're in Typescript, let's start by giving a type definition to our input.
Our type will have two fields, one to keep the original items we passed in, and the other to store its weight (or chance, probability).
A heavier weight means more likely to be picked up.
To add some more informative content, we'll build this up with a little bit of Test Driven Development (TDD).
We can create a new file, randomGenerator.ts with the skeleton of the final product.
Before writing the implementation, we have to write the tests! This approach helps write code that is more reliable and with fewer bugs as you focus on the business logic first, and only after that on the implementation.
In this case, I'm using Jest, so
npm i jest to have it running.
In my tests, I want to make sure that:
- The class is instantiated properly.
- A random item is picked and comes from my list.
- N random items are picked, exactly the amount I requested and all of them are in the list.
- Works not only with strings but also with objects.
Note: To improve the tests even more, there's an open issue about passing a custom random generator. This could allow to set a generator with a specific seed (Math.random can't do that) and make the tests predictable, rather than entirely random.
Now that the tests are written, we can proceed with the implementation.
If we run the test now, they're all green and 100% code coverage ✅
Let's add some validation to our input. As usual, tests first!
This time we want to check that:
- The list is not empty
- There are no duplicates
- There are no negative weights
Tests are looking good, we can implement the validation method.
npm run test and it's all green and 100% again! ✅
With the current implementation, to pick one item only you need to explicitly create the
RandomGenerator instance and call
pick on it.
As we're building a library, it might be useful to also provide some standalone methods that do everything under the hood so that one can directly call
pickMany without seeing the logic behind it.
The core implementation is done and the requested functionalities described at the beginning are already working.
As I mentioned, the library is Open Source and the code is available on GitHub. I'm repeating it because this means it's open to contributions and for example, you're free to pick up some of the open issues.
In the meantime, the library has been extended with more utility methods (
getCount) and also an extra standalone method to
flatten the items in case you have duplicates and you want to aggregate their weights.
Feel free to jump on GitHub and extend it, even more, I'm waiting for your Pull Requests! :)
Thanks for reading this post, I hope you find it interesting!
Feel free to follow me to get notified when new articles are out ;)
You can also follow me on your favourite platform!