A list class for weighted randomisation, 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((20, "sup"), (2, "salutations"));
Console.WriteLine(greetings.GetRandomValue());
// => salutations// TypeScript (under development)
let greetings = new WeightedList([20, "sup"], [2, "salutations"]);
console.log(greetings.select_value());
// => sup-- Haskell (under development)
greetings :: WeightedList String Int
greetings = newWeightedList [(20, "sup"), (2, "salutations")]
main :: IO ()
main = print (select greetings)
-- => salutations// Rust (under development)
let wl: WeightedList<String, i32> = WeightedList::from([
(20, "sup".to_string()),
(2, "salutations".to_string()),
]);
println!("{}", wl.get_random_value());
// => supAn immutable optimised variant FrozenWeightedList is also implemented, which provides
- Weighted randomised selection with a variety of constraints
- Utility methods to manipulate values and weights
- In-place and pure variants of methods for flexibility
- Conversions to and from a wide range of other data types
- Slice indexing1
Tip
For the full rationale behind this project, see rationale.
I made this class for weighted randomisation, where each element in a collection has a different chance of being selected – the greater an item’s weight, the higher the chance it is selected. This is super common in games for reward systems, displaying messages, etc.
Tip
Walkthroughs and specimens for each language can be found in their respective folders.
Honestly, I don’t trust my code enough to publish it :P
The project is not available as a package.2 Instead, just download the relevant files, or copy and paste the code directly.
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 WeightedListSee 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.
For a tutorial, see walkthrough.
- Made in Python 3.11
- No external dependencies
- Made in C# 12.0
- Supports LINQ querying
Under development!
Under development!
Under development!
Under development!
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. ↩