▶ Random Everything - 100+ Free Generators // v2.5.0
root@generate-random:~/random-number-csharp$ _

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#? ▶
Random.Shared.Next(1, 11) - the upper bound is exclusive. For a secure value: RandomNumberGenerator.GetInt32(1, 11).
Is System.Random secure? ▶
No. Use RandomNumberGenerator for anything security-related.
Why does my C# Random return the same numbers? ▶
On .NET Framework, Random instances created at almost the same moment get the same seed. Reuse a single instance or use Random.Shared.
Is Random.Next upper bound inclusive? ▶
No. Next(min, max) returns min to max - 1.

[ HOW TO CITE THIS PAGE ]

APA Style:
Generate-Random.org. (2026). C# Random Number Generator. Retrieved from https://generate-random.org/random-number-csharp
Web Citation:
C# Random Number Generator - Generate-Random.org (https://generate-random.org/random-number-csharp)