DEV Community

mohamed Tayel
mohamed Tayel

Posted on

3

Randomized Fun with Random.Shared.Shuffle and RandomNumberGenerator.GetString in C#

In .NET 8 and C# 12, handling randomness has become more efficient and robust. The introduction of methods like Random.Shared.Shuffle and RandomNumberGenerator.GetString offers developers convenient and secure options for generating random data, whether it's shuffling an array or creating random strings. This article explores these methods with practical examples, highlighting their benefits. The full code sample is provided in the first comment.

Shuffling Arrays with Random.Shared.Shuffle

The ability to shuffle an array efficiently can be crucial in many applications, such as gaming or data analysis. Previously, developers often had to implement their own shuffle algorithms. Now, Random.Shared.Shuffle simplifies this task significantly.

Example:
int[] array = { 1, 2, 3, 4, 5 };
Random.Shared.Shuffle(array);

foreach (var item in array)
{
    Console.WriteLine(item);
}
Enter fullscreen mode Exit fullscreen mode

This code snippet demonstrates how to shuffle an array using Random.Shared.Shuffle. The Random.Shared is a thread-safe, shared instance of the Random class provided by .NET, suitable for most general purposes.

Generating Random Strings with RandomNumberGenerator.GetString

Generating cryptographically secure random strings is essential for creating secure passwords, tokens, and identifiers. RandomNumberGenerator.GetString allows developers to generate such strings from a specified character set.

Example:
var allCharacters = "abcdefghijklmnopqrstuvwxyz0123456789";
var randomString = RandomNumberGenerator.GetString(allCharacters, 20);

Console.WriteLine(randomString);
Enter fullscreen mode Exit fullscreen mode

Here, RandomNumberGenerator.GetString is used to generate a 20-character random string from a specified set of characters. This method ensures the randomness is suitable for security-sensitive applications, which is crucial for avoiding vulnerabilities related to predictable random strings.

When to Use These Features

  • Random.Shared.Shuffle: Ideal for shuffling arrays in scenarios requiring random order, such as in randomized trials or gaming.
  • RandomNumberGenerator.GetString: Best used when cryptographically secure random strings are needed, such as for passwords, authentication tokens, or other security-sensitive functions.

Conclusion

.NET 8 and C# 12 introduce robust features like Random.Shared.Shuffle and RandomNumberGenerator.GetString that enhance the ease and security of working with random data. These improvements not only streamline the development process but also bolster the security and reliability of applications that depend on randomness.

Embrace these new capabilities in your .NET projects to simplify your code and enhance application security.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (2)

Collapse
 
moh_moh701 profile image
mohamed Tayel
Collapse
 
moh_moh701 profile image
mohamed Tayel

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️