Basics of System.Random in C#

February 14, 2020

System.Random Methods

C# and Unity

Random Class (from Microsoft)

Information – Link

By: Microsoft


General Methods Looked Into

Random.Next

This selects a random integer either between 0 and maximum value, or between two different designated values.

Random.NextDouble

This generates a random floating point value between 0.0 and 1.0. This is the main one I will be looking to use since I have a lot of parameters where floating point values make sense, however since it only returns values between 0.0 and 1.0 specifically, I will have to just use it as a random multiplication factor.

Random.NextByte

This requires an input of an array of bytes. It will then fill that array with random byte values.

Why I am Investigating

My thesis project uses a lot of random values with its focus on varied procedural content generation, but I want to control the randomness a bit more with the implementation of a seeding system. A tutorial I did a while back on cellular automata uses a very basic seeding system for its randomization that I was looking to copy. It centers around this line where seed is just a string variable that can be set in the editor:
System.Random psuedoRandom = new System.Random(seed.GetHashCode());

As long as the seed string is the same and a value is randomly selected using this Random variable psuedoRandom, the value selected for that particular case will remain the same over multiple plays of the program. This helps provide control and consistency to the random system generation so if I find good samples they can be recorded by their seed string.

Random.Next and Random.NextDouble are going to be the main two that I use, as they help generate random ints or random floating point values (doubles that can be cast as floats if necessary). Random.Next will be very useful in cases where I am generating a specific number of objects, where Random.NextDouble will be used to generate the varied values for parameters that are within specific ranges (such as the length or size of an object). As noted previously Random.NextDouble can only specifically generate values between 0.0 and 1.0, so it will be used as a random multiplication factor to get the results I need.