This page looks best with JavaScript enabled

Pseudo-Random Colors with Hashed Strings

 ·  ☕ 2 min read

In this video we’re going to explore a way I approach reproducible pseudo-random number generation that uses calculated hashes to select a random item or property for an object. In this case we’ll be assigning hue shifted colors to text strings. This helps easily identify elements in a list by searching for both a memorable name but also a color that will always be the same for a similar string.

To convert a string into an integer using an MD5 hash you could write something like this:

var stringToHash = "World of Zero";
var hasher = MD5.Create();
var hash = hasher.ComputeHash(Encoding.UTF8.GetBytes(stringToHash));
var hashedInteger = BitConverter.ToInt32(hash, 0);

Then to convert an integer into a random color use this. In this example 256 hues will be selected from the color wheel:

var color = Color.HSVToRGB((Math.Abs(hashedInteger) % 256) / 256.0f, 1.0f, 0.75f);

This same technique can be applied to other things as well. For example we can use the hashed value to index into a lookup table of names. Other approaches could apply a specific texture to a material or instantiate a specific prefab:

var potentialNames = new string[] { "World of Zero", "YouTube", "Unity 3D" };
var name = potentialNames[Math.Abs(hashedInteger) % potentialNames.Count];

The System.Security.Cryptography.MD5 class is documented here:
More information on how hashes work on Wikipedia:
Join the World of Zero Discord Server:

Sam Wronski
Sam Wronski
Maker of things and professional software engineer. Lets make something awesome together!