.NET 8 — New Randomness Methods

Henrique Siebert Domareski
3 min readJan 4, 2024

--

With .NET 8, new methods were introduced for working with randomness. In this article, I present how to use these new methods.

Random.Shared.GetItems Method

The System.Random.GetItems method allows you to randomly choose a specified amount of items from an input set.

In a previous .NET version, to achieve this we could do something like this:

var selectedValues = inputArray.OrderBy(_ => random.Next()).Take(amountToBeSelected).ToArray();

But now with .NET 8, we can make use of the GetItems method, which makes it much easier:

var selectedValues = Random.Shared.GetItems(inputArray, amountToBeSelected);

In the code below, there is an example of what happens when executing the GetItems method:

  • On line 1, there is an array of names.
  • On line 2, it prints the array with all the names.
  • On line 4, the GetItems method is executed, receiving as parameters the array of names and the amount of names to be selected. This method will return an array with two randomly selected items from the input array.
  • On line 5, the selected names are printed.

In the output we can see the names and the selected name:

Names: ["Bob","Ana","Jessica","Mike","Rick"]
Selected names: ["Ana","Rick"]

Random.Shared.Shuffle Method

The Random.Shared.Shuffle method allows you to shuffle elements in an array/span, without creating a new array/span, instead is only going to reorganize the items.

In a previous .NET version, to achieve this we could do something like this:

var random = new Random();
var shuffledArray = numbers.OrderBy(_ => random.Next()).ToArray();

Or also use the Fisher-Yates algorithm (also known as the Knuth shuffle), which is an algorithm for randomly shuffling the elements of an array:

int inputArrayLength = inputArray.Length;

for (int i = inputArrayLength - 1; i > 0; i--)
{
int j = random.Next(i + 1);
var temp = inputArray[i];
inputArray[i] = inputArray[j];
inputArray[j] = temp;
}

But now with .NET 8, we can make use of the Shuffle method, which makes it much easier:

Random.Shared.Shuffle(arrayInput);

In the code below, there is an example of what happens when executing the Shuffle method:

  • On line 1 the array of numbers is created.
  • On line 2, the array value is printed.
  • On line 3, the Shuffle method is executed.
  • On line 4, the Shuffle numbers are printed.

In the output we can see the numbers before and after shuffling:

Numbers before shuffling: [1,2,3,4,5]
Numbers after shuffling: [2,4,5,1,3]

RandomNumberGenerator

The RandomNumberGenerator was already present in previous .NET version, but was extended in .NET 8 with the GetString method.

The RandomNumberGenerator comes from the namespace System.Security.Cryptography, and it creates cryptographically strong random values. This class is preferable to be used when dealing with sensitive data (it is a bit slower than System.Random).

There is a range of methods available in this class, in the example below you can check the GetString method.

var randomString = RandomNumberGenerator.GetString(someString, stringLength);
  • The GetString method receives a string and a string length as parameters, and uses this input string to create a new string with the specified length. It creates a string populated with random characters chosen based on the input with a specified length.

In the code below, there is an example of what happens when executing the GetString method:

  • On line 1, the GetString method is executed, and it receives a string and the string length as parameters.
  • On line 2, the result is printed.

In the output we can see the cryptographic secure string that was created:

Random number generator: "e8letHeeol"

For other methods from RandomNumberGenerator class, you can check the official Microsoft documentation: RandomNumberGenerator — Microsoft Docs.

Conclusion

The methods from System.Random.Shared provide useful functionality in a performative way, saving you from having to write your own methods to randomly shuffle or randomly select values, and the methods from System.Security.Cryptography`.RandomNumberGenerator can also be used to generate cryptographic secure values.

This is the link for the project in GitHub: https://github.com/henriquesd/DotNet8RandomnessMethods

If you like this demo, I kindly ask you to give a ⭐️ in the repository.

Thanks for reading!

--

--