anarchy

Incremental & reversible chaos library. Generates pseudo-random numbers (not suitable for cryptographic or statistical applications) in a reversible and incremental manner.

“Reversible” means that you can take a random result value and use reverse random functions to determine the seed value it came from, or take a shuffled item position and determine which original position it had. This property is useful for implementing things like a compass or ‘rare item radar’ that can lead the way to the nearest randomly-distributed item of a particular type, since you can not only ask “I’m generating the tile at 3, 17, what goes here?” but also “Which tile was treasure #25 assigned to?”

“Incremental” means that you can generate the index of one shuffled item without generating and storing an array containing the total ordering of the items (or figure out which segment an item got distributed into without storing the full mapping of items to segments). This property is useful in situations like Minecraft, where we need to generate chunks of a larger world based on the order the player explores them in. Pre-generating entire regions (or even data structures to support placement of items in those regions) is not feasible, and instead with anarchy, you can simply request the portions of a shuffle or distribution that you need as you need them. In general this also means that you don’t need to manage and hand around an “RNG” instance in the code, but can instead share a single integer seed and have each part of the code derive all the information it needs from that alone.

Capabilities include:

The core API is available in C, Python, Javascript, and Unity/C#, and includes:

Core documentation

Reading the source code comments for the implementation you’re using is the best way to get details on specific functions. Online you can find:

Each individual implementation’s source code is documented.

Example code

Example code using the JavaScript implementation that demonstrates things to do with the library. These examples run live in your browser and include code snippets showing how things are accomplished; reading through these is the fastest way to get a sense of how to use the library and what it’s good for.

Examples

Unity Example

The unity/anarchy folder is a complete Unity project which includes a component implementation of the library and a simple demo component that uses it to shuffle tiles in a Tilemap. When it’s running, you can left-click to advance to the next seed, or right-click to go back to the previous seed. The code in the Tilepicker component demonstrates how to use anarchy.

That said, I’m not a Unity expert and this probably isn’t the right way to release the library; it seems like a plugin would be more appropriate, but I haven’t yet figured out the details of building and deploying one. If you’d like to use anarchy more seriously in a Unity context, let me know (even just by opening an issue) and I can make that more of a priority.