C# Random Number Generator
Random numbers in C#: a random integer in any range, floats, picking and shuffling, and secure random values - with ready-to-paste code
How to generate a random number in C#: Random.Shared for everyday use, RandomNumberGenerator for anything security-related. Choose a range above for ready-to-paste code.
Random Int Between Min and Max
Next(minValue, maxValue) excludes maxValue, so add 1 to include it. Random.Shared is thread-safe (.NET 6+).
int n = Random.Shared.Next(1, 11); // 1..10
int m = Random.Shared.Next(10); // 0..9
Random Double
NextDouble() returns a value in [0.0, 1.0).
double d = Random.Shared.NextDouble(); // 0.0 <= d < 1.0
double e = 1.5 + Random.Shared.NextDouble() * 3.0; // 1.5 <= e < 4.5
Random Element and Shuffle
Pick by index; .NET 8 added Shuffle on both Random and RandomNumberGenerator.
string[] items = { "red", "green", "blue" };
string pick = items[Random.Shared.Next(items.Length)];
Random.Shared.Shuffle(items); // .NET 8+
RandomNumberGenerator.Shuffle(items.AsSpan()); // secure, .NET 8+
Secure Random Numbers
System.Random is predictable. For tokens, passwords and draws use System.Security.Cryptography.RandomNumberGenerator.
using System.Security.Cryptography;
int n = RandomNumberGenerator.GetInt32(1, 11); // 1..10
byte[] token = RandomNumberGenerator.GetBytes(16);
Common Mistakes
On .NET Framework, creating several new Random() in quick succession can give them the same time-based seed and identical sequences - use one shared instance (or Random.Shared on .NET 6+).
Frequently Asked Questions
How do I generate a random number between 1 and 10 in C#? ▶
Is System.Random secure? ▶
Why does my C# Random return the same numbers? ▶
Is Random.Next upper bound inclusive? ▶
[ HOW TO CITE THIS PAGE ]
Generate-Random.org. (2026). C# Random Number Generator. Retrieved from https://generate-random.org/random-number-csharp
C# Random Number Generator - Generate-Random.org (https://generate-random.org/random-number-csharp)