Unity RNG Package
This is a package that's intended to be a complete replacement for Unity's RNG tools. It's an extremely lightweight hash-based pseudo-random number generator built on top of Squirrel Eiserloh's Squirrel 3 hashing algorithm (which he talks about in his great 2017 GDC talk). It comes with a ton of common utilities, all of the integer-based operations are entirely cross-platform deterministic (a requirement for its use in fighting game engine), and ideally when released will come with unit tests and in-engine visual demonstrations.
Features
- Instanced but has a static instance for when u really don't care
- Its state is just two unsigned integers, one for the seed and one for the position
- It's easily serialized
- All methods that work in integers specifically are wholly deterministic cross-platform and use no floating point math
- Has a ton of handy methods
- Comes with a super sick grab back implementation
Methods
- Utility methods like
InitState()
,Clone()
,CopyStateTo(otherRNG)
, etc Range()
functions withfloat
andint
overloads- (All of the int overloads are deterministic so I had to write a deterministic integer Lerp method which I'm proud of)
Shuffle()
shuffles arrays and listsChance(probability)
Pick(list/array)
picks a random item from the array, also has overloads for providing weights to the elements or a function for retrieving weights for a given elementPickIndex()
picks a random index given an array of weights, this is separated out from the arrays cause sometimes you just want the numberGetColorRGB/A()
GetVector2/3/4()
Noise1/2/3D(position)
generates a noise value at a given positionPointInUnitCircle/Sphere()
andPointOnUnitCircle/Sphere()
all of these solve the "density in the middle" problem with a cool solution for generating the random radius I found from this video. I extrapolated parts of it and combined it with a method for generating spherical coordinates on Karthik Karanth's blog post about itPointOnRectangleEdge(rectangleExtents/Rect)
andPointInRectangle(rectangleExtents/Rect)
this I actually found from 2020 Noé looking up instances when I've said "random" on Twitter, and saw that Tom Francis suggested I could improve the random edge stick selection probability based on the ratio between the side lengths! Thanks Tom I'm finally doing that 4 years later hehe- Also note the Rect overloads cause I've been finding myself using the Rect class more than usual the past few months so these take into account the Rect's position when returning the point on the edge or inside
PointOnBoxFace(boxExtents)
andPointInBox(boxExtents)
Two things that I think would be cool to implement are animation curve biases and the choosing a random subset of a list which is talked about in the Unity manual page for the Random class.