# Algorithm Implementations of Random and PerlinNoise

I have been working on a personal project on-off for a few months now. It is a dedicated server for Valheim called Valhalla, written in c++ (GitHub - ricosolana/Valhalla: Valheim server written in C++). One of the primary mechanics requires the included Unity Random class and PerlinNoise function for consistency between the server and the client when provided a world generation seed (terrain generation, height map, etc…).

I have scoured the internet high and low within the limits of my abilities, and have found frighteningly few sources on the exact algorithm implementation that the Random class and PerlinNoise functions use. Here are my findings: UnityCsReference, a forum post, and the C# documentation for Random and Perlin Noise. The Mathf.PerlinNoise function likely uses a variation of the algorithm by Ken Perlin. Unity Random uses the Xorshift 128 algorithm to generate random numbers, but again, the implementation varies. I thought I could just create a texture to hold precomputed Unity PerlinNoise generated values, but I was wrong because I assumed the input values had to be within 0 to 1 (they are not).

I have no financial motive for requesting this information. This project is a totally personal hobby of mine, formed out of self-interest. I could always try spending countless hours reverse engineering the exact algorithm implementation with AI and pattern recognition— something that I have sparse knowledge of. Doing this on my own would be like trying to find a needle in a haystack. By native code, I am referring to the under-the-hood calls made by C# scripts to the internal UnityEngine. These functions are native :

Let me be clear in saying that I am not looking for the proprietary native code for these implementations (although that would be nice). I am simply looking for an algorithm that produces identical results to the Unity implementation; an algorithm consistent with that of the Random/Mathf.PerlinNoise algorithms. Native code or accurate pseudocode, I have no preference. If someone can point me toward a meaningful direction to how exactly Unity implements these, that would be grand.

TLDR; I am looking for an algorithm for Random and PerlinNoise that spits out values equal to the Unity counterparts.

The documentation forum isn’t the place to discuss this. The documentation team won’t be able to provide any insight into source access or algorithm implementation. I’ll move your post to the scripting forum.

for exact results you’d need the native code as obviously even the smallest change might give different results. Nevertheless, all I can really post are the references that the source implementation cites:

// Xorshift 128 implementation
// Xorshift paper: http://www.jstatsoft.org/v08/i14/paper
// Wikipedia: http://en.wikipedia.org/wiki/Xorshift

The wikipedia article looks pretty similar to some of the code although note that I have don’t know anything about the details of the implementation itself.

I managed to reverse engineer the algorithm for both Random and Mathf.PerlinNoise.

PerlinNoise is basically a port of Ken Perlins Noise algorithm with some output differences https://cs.nyu.edu/~perlin/noise/. Here is my implementation Valhalla/src/VUtilsMath.cpp at dcfedcdd1563181956e9fc518f687274c946b2a9 · ricosolana/Valhalla · GitHub.

For Random, someone already appeared to have recreated the algorithm, but I didn’t get to see it before I did this C# implementation of UnityEngine.Random with (almost) 1-to-1 parity · GitHub. My implementation Valhalla/src/VUtilsRandom.cpp at steam-server · ricosolana/Valhalla · GitHub with several other functions. Comparative results:

I have not extensively tested the above, but as you see, they are very similar.
I hope this helps anyone who was in my prior position.