Generating Random Numbers in C# 14

Generating random numbers is a common task in software development, whether for simulations, games, testing, security operations, or simple utility functions. C# provides multiple ways to produce random values, ranging from the classic Random class to more modern, cryptographically secure APIs.

This Post walks through the different methods to generate random numbers in C# 14, explains how they work, and offers best practices for real-world usage.

Generating Random Numbers in C# 14

Getting Started

Generating random numbers is a fundamental task in programming, used in everything from games and simulations to data sampling, testing, and security. In C#, developers have access to several built-in tools that make random number generation both simple and powerful. Whether you need quick, lightweight randomness for everyday applications or cryptographically strong values for secure operations, C# provides a solution. This article explores the various techniques for generating random numbers in C#, explains when to use each method, and offers practical code examples to help you get started

Using the Random Class (Classic Method)

The most common and simplest way to generate random numbers in C# is by using the built-in Random class in the System namespace.

Example: Generate a random integer
var random = new Random();
int number = random.Next();     // Generates a non-negative integer
Console.WriteLine(number);

Generate a number within a range
int number = random.Next(1, 101);   // Generates 1–100

Generate a random double
double value = random.NextDouble();  // Returns between 0.0 and 1.0

Avoiding a Common Pitfall: Re-seeding Random

Beginners often write code like this:
int n1 = new Random().Next();
int n2 = new Random().Next();

Because Random seeds itself based on the system clock, rapidly creating new instances results in identical sequences—not truly random.

Correct approach
var rnd = new Random();
int n1 = rnd.Next();
int n2 = rnd.Next();

Using RandomNumberGenerator (Cryptographically Secure)

For security-sensitive applications (passwords, tokens, encryption keys), use System.Security.Cryptography.RandomNumberGenerator.

Generate a secure random integer
using System.Security.Cryptography;
int number = RandomNumberGenerator.GetInt32(1, 101);
Console.WriteLine(number);

Generate secure random bytes
byte[] buffer = new byte[16];
RandomNumberGenerator.Fill(buffer);

This method provides unpredictable, cryptographically strong random values.

Using Random.Shared (C# 10+ Optimization)

Modern .NET versions provide a thread-safe, shared instance:
int value = Random.Shared.Next(1, 100);

This avoids manually managing instances and is efficient in multi-threaded scenarios.

Generating Multiple Random Numbers

Example: 14 random numbers between 1 and 100
var rnd = new Random();
var numbers = new List<int>();
for (int i = 0; i < 14; i++)
{
  numbers.Add(rnd.Next(1, 101));
}
Console.WriteLine(string.Join(", ", numbers));

When to Use Which Method
Scenario Recommended Method
Games, simulations, basic randomness Random or Random.Shared
Web apps, multi-threading Random.Shared
Cryptography, authentication RandomNumberGenerator
Needing random floats/doubles Random

This C# 14 code, which uses the built-in Random class to generate random numbers, can be easily integrated into any random-number generator app or software that supports C# 14 and .NET 10. By creating an instance of Random—for example, Random rnd = new Random();—the application can produce new values whenever needed. Methods like rnd.Next() generate integers within a chosen range, while rnd.NextDouble() provides floating-point numbers between 0.0 and 1.0. This makes the code highly versatile for different scenarios, including simple number-picking tools, games, simulations, and testing utilities. Because the Random class is lightweight and straightforward to use, developers can quickly implement reliable randomness in their applications without requiring external libraries or complex logic.

Summary

C# 14 provides flexible ways to generate random numbers, from the simple and efficient Random class to secure cryptographic generators. Understanding which approach matches your scenario will help you write safer and more reliable code.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال