This week, research was conducted on random number generation and noise.

True randomness is impossible to achieve in code as each generated number is deterministic, “no number produced by a mathematical operation is truly random” (Bridle, 2022). To emulate randomness, programs often make use of pseudo-random number generators (PRNGs). These are algorithms that produce sequences of deterministic numbers, “the sequences have no discernible pattern, so they seem random” (Rowe, 2020). These algorithms are supplied with a starting value, called a seed, which is used to initiate the sequence. However, if the algorithm is supplied with the same seed it will produce the same sequence of numbers (Rowe, 2020). Typically, seeding is done by taking the system clock as it’s value, but more complex methods can be done to ensure better randomness.

A good example of this is Cloudflare’s “Wall of Entropy”. In the Cloudflare lobby, there is a wall arranged with about 100 lava lamps with a mounted camera pointing at them. “The camera takes photos of the lamps at regular intervals and sends the images to Cloudflare servers.” (Cloudlfare, n.d.).

(Cloudflare. n.d.)

These images become a string of numbers that are used as the seed for encryption keys. As the “lava” in these lamps never take the same shape twice, the strings generated from the images taken are always different, ensuring that each encryption key is unique (Cloudlfare, n.d.).

By making use of Random.Range() in Unity, we can implement a basic PCG to create terrain.

Here heights for the the vertices will be generated using Random.Range() using minHeight and maxHeight as constraints. These values will be set later in the Unity editor.

Here the smoothness of the terrain is adjusted by averaging the heights of the neighbouring vertices.

Here the terrain has been generated by making use of Unity’s Mesh class.

We can alter the generated terrain by changing these values within the Unity editor.

Finally, here is an example of a terrain generated from this code.

This implementation of PRNG has been done through the use of noise. “Noise is a set of random values over some fixed interval” (Nevala 2022). Static noise is a type of noise where each value is disconnected from eachother, whereas smoothed noise is a type of noise where there is continuity between the values (Nevala 2022). Smoothed noise was used in the implementation by adjusting the values of the vertices based on the average heights of neighbouring vertices - adding continuity between the values.

Another way in which noise can be used for game development in through texture generation. A popular method by doing this is through the use of Perlin Noise. “Perlin Noise is an extremely powerful algorithm that is used often in procedural content generation. It is especially useful for games and other visual media such as movies. In game development, Perlin Noise can be used for any sort of wave-like, undulating material or texture” (Biagioli 2014).

"By applying a simple gradient, a procedural fire texture can be created" (Biagioli 2014)

Perlin Noise can be implemented in Unity through the use of Mathf.PerlinNoise() (Unity, n.d).

This script generates a Perlin noise texture and dynamically applies it to a GameObject’s material every frame. It starts by defining the texture size (width and height) and the level of detail (scale). In the Update method, it retrieves the object’s Renderer and assigns a new texture to the material. The GenerateTexture method creates a Texture2D, loops through each pixel, calculates a grayscale color using Perlin noise, and sets the pixel color. The CalculateColor method converts pixel coordinates into normalized Perlin noise coordinates, computes a noise value using Mathf.PerlinNoise, and returns a corresponding grayscale color.

Here we can change the width, height and scale of the generated texture.

And here is the result of the implementation.


Reference List

Bridle, J. (2022). Why Computers Can’t Generate Randomness. Slate [online]. Available from: https://slate.com/technology/2022/06/bridle-ways-of-being-excerpt-computer-randomness.html [accessed 2 October 2024].

Rowe, E. (2020). Generating Predictable Random Numbers in Unity. Red Blue Games Blog [online]. Available from: https://blog.redbluegames.com/generating-predictable-random-numbers-in-unity-c97b7c4895ec [accessed 2 October 2024].

Cloudflare. (n.d). How do lava lamps help with Internet encryption?. [online]. Available from: https://www.cloudflare.com/learning/ssl/lava-lamp-encryption/ [accessed 2 October 2024].

Unity. (n.d). Random.Range (Unity API). [online]. Available from: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Random.Range.html [accessed 2 October 2024].

Unity. (n.d). Mesh (Unity API). [online]. Available from: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Mesh.html [accessed 2 October 2024].

Zoltikrys (2024) ProceduralGrid. GitHub. Available from: https://github.com/Zoltikrys/ProceduralGrid [accessed 2 October 2024].

Nevala, E. (2022). Noise Generation. gamedev.net [online]. Available from: https://www.gamedev.net/tutorials/programming/math-and-physics/noise-generation-r3793/ [accessed 2 October 2024].

Biagioli, A. (2014). Understanding Perlin Noise. adrian’s soapbox [online]. Available from: https://adrianb.io/2014/08/09/perlinnoise.html [accessed 2 October 2024].

Unity. (n.d). Mathf.PerlinNoise (Unity API). [online]. Available from: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Mathf.PerlinNoise.html [accessed 2 October 2024].

Zoltikrys (2024) PerlinNoiseTexture. GitHub [online]. Available from: https://github.com/Zoltikrys/PerlinNoiseTexture [accessed 2 October 2024].

Updated: