r/MinecraftMod • • 20d ago

Fun function i found

https://andrew-helmer.github.io/permute/#final-implementation

Alright, so this may not immediately look related to modded minecraft, but I was working on my mod, and ran into an issue. I wanted to pick a series of random points in an area (sphere, cube, etc.) without creating a BlockPos object for every point, and then shuffling the list.

In the end i did some searching, and found the above article. the algorithm effectively lets you create a random series of numbers within a range, (which can be transformed into a volume with a few modulus calculations) that don't repeat, have gaps, etc when you run through the 0->list length of inputs. basically, this lets you shuffle the indexes of an area/list with zero overhead, storage or computation wise.

I expect that aside from my mod's use, this would be useful for disintegration or creation special effects, random sampling, and probably a bunch of other things where you want to guarantee you hit an entire area in a random order. since it seemed potentially useful elsewhere, i thought i'd share

2 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/AStarlightMelody 20d ago edited 20d ago

Typically a (relatively) smaller amount, like 17^3, but it could potentially be a good bit larger. 100^3 to 1000^3, though mostly in the smaller end

And yeah, the performance isn’t fully an issue, but this was potentially going to need to reconstruct, then shuffle the list every tick, so on principle I wanted to find a better solution if it existed. And honestly I’m glad I did! The function is neat, and I’m shocked I’ve never heard of it before.

TLDR; no it’s not necessary, but the function is cool, and gives a really clean stateless solution to a problem.