A class representing a list of weighted items, implemented (eventually) in every language I’ve learnt.
# Python
greetings = WeightedList(
(20, "sup"),
(2, "salutations"),
)
print(greetings.select())
# sup
// C#
WeightedList<string, int> greetings = new(
(2, "sup"), (20, "salutations")
);
Console.WriteLine(greetings.GetRandomValue());
// salutations
Tip
For the full rationale behind this project, see rationale.
Mainly intended for weighted randomisation, where each element can have a different chance of being selected (its weight).
The prime example of this is lootbox or reward systems in games, where items have different rarities.
- Randomised selection with a variety constraints
- Convenience methods to manipulate values and weights
- Separate methods with and without side effects for flexibility
- Conversions from and to a wide range of other data types
- Slice indexing1
The project is not available as a package.2 Instead, just download the relevant files, or copy and paste the code directly.
Tip
Walkthroughs and specimens for each language can be found in their respective folders.
All you need is the weightedlist.py
file, which contains the WeightedList
class with all the functionality. Simply import it, and you’re ready to go!
from weightedlist import WeightedList
See walkthrough for a tutorial, or examples for examples.
All the code is contained within the WeightedList.cs
file. You might also need the weighted-list.csproj
file.
If you want the entire solution, you can download the repo and extract the c-sharp/
folder. This also contains tests to verify that everything is working.
For a tutorial, see walkthrough.
- Made in Python 3.11
- All imports are from the standard library, so there are no external dependencies
- Made in C# 12.0
- Supports LINQ querying
- Due to the nature of weighted indexing, random access has a time complexity of
$O(n)$ , where$n$ is the number of elements in the list.- However, optimising this further without significant sacrifices in space complexity (which is already decently hefty) appears unviable.
This project is licensed under the MIT license, so feel free to use it however you wish (although some credit would be cool!).
Back when I was picking up the ropes of Python, I was working on a project which featured randomisation, and, like any game developer, I thought it’d be cool to give each outcomes different probabilities of occurring. At first, I achieved this behaviour by duplicating items, but I quickly realised the numerous issues with this.
And so, I set out to write my own class, which I’d never really needed to do up until that point. I thought it’d be a great exercise in learning Python – and it very much was, teaching me tons about object-oriented programming, dunder methods, generators, etc. It was also my first experience of conscientiously writing code that wasn’t exclusively for myself, which helped me understand the importance of consistency and clarity, and above all, documentation.
A couple years later, I’ve come back to do the same in C#, this time also adding several features I always intended to add but never did – especially non-integer weights, which allows the class to truly embrace its usage as representing probabilities. Trying to translate Python into C# was an interesting experience,3 and helped highlight some important differences between the languages that I would otherwise not have found out.
I mean yeah, a whole several-hundred-lines class to handle one thing might be a bit overkill. But it’s far more convenient to have it all packaged this way into a single portable file that can easily be slotted into other projects. Regardless, I’ve used my own code4 in at least 2 major projects (PENGUIN↗ and Algorhythm↗), so I can definitely say it’s been useful to me!
1, documentation; 2, line breaks; 3, extra functionality. Particularly documentation. That stuff just eats the line count. Also, implementing something as complex as an enumerable container requires a lot of methods, operators and interfaces, both in Python and C#. And in C# you've even got overloading to account for as well.
I have tried to ensure everything is implemented as efficiently as possible, but I cannot guarantee every single part is perfectly optimised, and I haven’t gone to the extremes of timing different approaches. Operations that may take longer than expected will likely have that mentioned in their documentation (docstrings for Python, XML comments for C#).
I have my own particular preferences when it comes to coding in Python, which I explain fully here↗.
Any feedback, suggestions or improvements are definitely welcome!
Footnotes
-
Really quite difficult with non-integer weights. ↩
-
I don’t think it’s a large enough project to warrant an entire package, when you could just copy and paste the code directly. ↩
-
This was not exactly the way I created the project in C#, but the Python implementation certainly laid out a general framework and was influential in some design decisions. ↩
-
To my own surprise, somewhat. ↩