Generate Random Numbers in C#
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C#
What: Generate random numbers in C# using the <code>Random</code> class for general purposes or <code>RandomNumberGenerator</code> for cryptographically secure random numbers in .NET applications.
Try it: Use our interactive Numbers generator or integrate this code into your C# application.
Generate random numbers in C# using the Random class for general purposes or RandomNumberGenerator for cryptographically secure random numbers in .NET applications.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
Java
,
C++
,
Ruby
and
Go
or use our interactive web generator.
C# Code Example
using System;
using System.Security.Cryptography;
using System.Linq;
class RandomNumberExample
{
static void Main()
{
// Generate a single random number between 1 and 100
Random random = new Random();
int randomNumber = random.Next(1, 101);
Console.WriteLine(randomNumber); // Example output: 42
// Generate multiple random numbers
int[] numbers = Enumerable.Range(0, 10)
.Select(_ => random.Next(1, 101))
.ToArray();
Console.WriteLine($"[{string.Join(", ", numbers)}]");
// Example output: [23, 7, 91, 45, 12, 68, 3, 89, 54, 31]
// Generate cryptographically secure random number
using (var rng = RandomNumberGenerator.Create())
{
byte[] bytes = new byte[4];
rng.GetBytes(bytes);
int secureNumber = (Math.Abs(BitConverter.ToInt32(bytes, 0)) % 100) + 1;
Console.WriteLine(secureNumber); // Example: 67
}
// Random double between 0 and 100
double randomDouble = random.NextDouble() * 100;
Console.WriteLine($"{randomDouble:F2}"); // Example: 42.17
}
}
[EXPLANATION]
Random.Next(min, max) returns a random integer from min (inclusive) to max (exclusive). For cryptographic applications, use RandomNumberGenerator which provides cryptographically strong random bytes. LINQ makes it easy to generate arrays of random numbers.
Expected Output
42 [23, 7, 91, 45, 12, 68, 3, 89, 54, 31] 67 42.17
Common Use Cases
- .NET application random data generation
- Unity game development (procedural generation)
- ASP.NET security token generation
- Desktop application (WPF/WinForms) features
- Azure Functions and cloud services
- Test data generation for unit tests
Important Notes
-
Randomis NOT thread-safe - create instance per thread -
Use
RandomNumberGeneratorfor cryptographic security - Available in .NET Framework, .NET Core, and .NET 5+
-
Random.Next(max)is exclusive (0 to max-1)
Try Our Interactive Generator
Don't want to write code? Use our free web-based Numbers generator with instant results.
TRY NUMBERS GENERATOR →Other Programming Languages
View Numbers generation code examples in PHP
View Numbers generation code examples in JavaScript
View Numbers generation code examples in Python
View Numbers generation code examples in Java
View Numbers generation code examples in C++
View Numbers generation code examples in Ruby
View Numbers generation code examples in Go