r/algorithms • • 9d ago

Discussion Lustro: Randomness as a Tree, Not a Sequence

Hi everyone. I would like to share Lustro, a 256-bit engine I created.

One of the main ideas behind Lustro is treating randomness as a tree, not a sequence of rigid values. I personally see it as a wave function. I'm a chemist by education. The basic problem is familiar: with a conventional sequential RNG the output you get depends on how many values have already been consumed. Adding one random operation can shift the sequence.

Lustro allows to derive streams from a seed and a path of identifiers. It can switch between single lane and batch processing.

It allows giving every object its own stream and can change how one of them is generated without affecting others. Full snapshot support is built in. The parent stream is not affected by forking and each child starts its own trajectory. Each branch has its own state and can be advanced independently.

The result is a tree-like structure of deterministic streams. Each branch can be advanced independently and you can reproduce a particular branch directly from its path.

Some specs. A unified stream engine combining hash, prng and xof. All based on one internal transformation mechanism. Scalar/AVX2. Written natively in Rust, it also comes with python and C layers.

The main repo contains all the test results. The generator is currently considered non-cryptographic.

github main repo
https://github.com/Ligatum/Lustro_API

cargo
https://crates.io/crates/lustro_api

pypi
https://pypi.org/project/lustro-api/

It's an independent project. I'd appreciate constructive feedback. I'm planning to improve on the API with time, ideas and feedback are welcome. Thanks.

9 Upvotes

12 comments sorted by

2

u/dorox1 6d ago

This sounds like a cool way to do it. Could you maybe provide a use-case example? I'm trying to figure out where I would want to use this rather than a growing array of independent generators.

Is the goal a performance improvement, or does the tree/update structure have a practical purpose of its own?

2

u/atomU235_3113 6d ago

I wouldn’t describe it primarily as a performance improvement although the performance is good. I'm getting 27GB/s on an i5-11600K @ 4.5Ghz constant with stream batch processing. The generator scales very well with multithreading. There is also a speed tester in the API repo for anyone who wants to do quick check. The main purpose is the tree structure itself and how streams are derived and managed.

Ok. Let's take a quick procedurally generated world as an example. You could have a hierarchy like world > chunk > vegetation > tree and separately world > chunk > terrain. Each part has its own stream addressed by a path of identifiers:

from lustro import LustroPrng

seed = bytes(32)
VEG, TERRAIN = 1, 2

def veg(chunk, tree): return LustroPrng.derive_path(seed, [0, chunk, VEG, tree])
def terrain(chunk): return LustroPrng.derive_path(seed, [0, chunk, TERRAIN])

Now - you can regenerate one chunk or a single tree without having to generate anything before it. An extra random call in the vegetation code doesn’t shift the terrain of that chunk or actually any other chunk. You can also generate chunks in any order and get the same results. That is as long as the seed and the identifier paths are the same.

One caveat to watch out for with forking: the child depends on the parents current block so for layouts that must be reproducible derive from the seed with derive_path instead of forking after consuming values. Fork simply depends on the parnets current block and not the cursor position inside the block.

In Lustro this hierarchy is part of the API. Hope that helps.

2

u/dorox1 6d ago

Great explanation, thank you! My first thought was procedural generation, and I see how this is a nicer approach than managing your own hierarchy and independence-levels for this stuff.

Performance does sound pretty good, though (not that I have a baseline to compare it to).

1

u/atomU235_3113 6d ago

Happy to help.

3

u/Phytor_c 7d ago

The “x not y” reads like LLM writing to me…

3

u/bartekltg 7d ago

LLM overusing an expression doesn't mean people do not use it.
And here is seems to describe a real difference between this and regular PRNGs, not just a flavor text (see, here is that template again!).

I haven't looked inside, but the first question from me would be, how to even test it. We have a very decent test for random streams (TestU01, diehard(er), probably something new appeared), but now we may branch, including many times, and it would be nice to see if there are some "correlations" between branches.

Results from TestU01 or similar, just for the single stream, would be also nice. No one taking PRNG seriously wont touch a generator that wasn't tested at all.

0

u/atomU235_3113 6d ago

How to test branching streams is the exact question that's been on my mind for a long time now. I don't really have a good answer to that.

The mechanism contains dynamic transformations with the data defining its own trajectory. The next best thing I could do is single streams testing in available suites. BigCrush, PractRand and NIST tests. I have all the results aggregated in the main repo. I tested both the full state and the state divided into slices.

1

u/bartekltg 6d ago

For different branch, at leas some correlation would be nice. Signal from one * a second signal delayed by n (for many n). This is IMHO too little, but it would at least catch some obvious problems

At least for now I see it as a convenience. I have a set of processes/threads and can give each one an independent generator. But for now I (and everybody else) when there was a need for 235246 threads calculating the same stuff (like you do for monte carlo) choosing a random seed for each one was a default option (the birthday paradox make it a bit less efficient, but there are generators with loooong period) or you could use a generator that can skip n numbers in time much smaller than O(n), thus shifting the same generator to not overlapping region for all threads.

> The mechanism contains dynamic transformations with the data defining its own trajectory. 

What data? IF the data is "bad" do I get bad numbers?

> The next best thing I could do is single streams testing in available suites.

Yep. It would be a great idea.

0

u/atomU235_3113 6d ago

Just to clarify. I'm Polish and I think there was some linguistic mishap.

> What data? IF the data is "bad" do I get bad numbers?

Absolutely not. What I meant is the algorithm contains two seperate, complimentary mechanisms. One is responsible for directly processessing the input, the other one is a control unit that derives the two final state rotations (based on the output of the primary transformation module). It's a deterministic control unit that self-parametrizes dynamically.

As for single lane testing - I've already done that and uploaded in the main repo. All results are available there.

2

u/dorox1 6d ago

It's usually a sign of LLM writing, but that feels like a really natural place for it even in human writing. The sentence is explaining how it's different from existing methods of doing the same thing

1

u/Interesting_Debate57 4d ago

If vectors generated by it don't lie on hyperplanes, it might be useful for something, but generally unless you're a mathematician I don't think it's advantagous to try to develop your own PRNG. There will be horrific failure modes unless you've copied someone else's design.

1

u/atomU235_3113 3d ago

That's a fair point. I absolutely agree that rolling your own PRNG comes with risks. That's why I also put a lot of work into testing the mechanism. I have both standard test suites like BigCrush and PractRand along with my own structural tests. All performed on raw engine state.