Generating a random decimal in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C# provides several ways to generate random decimal numbers. Random.NextDouble() returns a double between 0.0 and 1.0, which you can scale to any range. For the decimal type specifically (needed in financial calculations), there is no built-in NextDecimal() method, so you build one from random bytes or by combining random integers. For cryptographically secure random numbers, use RandomNumberGenerator instead of System.Random.
Random Double (0.0 to 1.0)
Random Double in a Custom Range
The formula min + (NextDouble() * (max - min)) generates a uniform random value in [min, max).
Random Float
Random Decimal
System.Random has no NextDecimal() method. Build one from random components:
Method 1: Scale from Double
Method 2: From Random Bytes (Full Precision)
Method 3: Decimal in Range with Precision
Cryptographically Secure Random
System.Random is a pseudorandom number generator (PRNG) — predictable given the seed. For security-sensitive applications, use RandomNumberGenerator:
Thread Safety
System.Random is not thread-safe. In multi-threaded code:
Generating Random Decimal Arrays
Common Pitfalls
- Creating new
Random()instances in a tight loop:new Random()seeds from the system clock. Multiple instances created within the same millisecond produce identical sequences. Use a single instance orRandom.Shared(.NET 6+). - Assuming
NextDouble()can return 1.0:NextDouble()returns values in[0.0, 1.0)— the upper bound is exclusive. The maximum value is 0.99999999999999978. If you need an inclusive upper bound, add a small epsilon to the range. - Using
Randomfor security-sensitive purposes:System.Randomis predictable if the seed is known. UseRandomNumberGeneratorfromSystem.Security.Cryptographyfor tokens, passwords, or any security-related randomness. - Casting
decimaltodoublefor range calculation: Thedecimaltype has higher precision (28-29 significant digits) thandouble(15-16 digits). Casting todoublefor intermediate calculations loses precision. For financial applications, use integer-based approaches. - Sharing
Randomacross threads without synchronization:System.Randomis not thread-safe. Concurrent access can corrupt internal state, producing all zeros or throwing exceptions. UseRandom.Shared,ThreadLocal<Random>, or locking.
Summary
- Use
random.NextDouble()for a random double in[0.0, 1.0), scale withmin + NextDouble() * (max - min)for custom ranges - Use
random.NextSingle()(.NET 6+) for random floats - Build
NextDecimal()from random integers or scale fromNextDouble()for thedecimaltype - Use
RandomNumberGeneratorfor cryptographically secure random numbers - Use
Random.Shared(.NET 6+) orThreadLocal<Random>for thread-safe random generation
Related reading
- Generating a random, non-repeating sequence of all integers in .NET
- Generating Unique Numeric IDs using DateTime.Now.Ticks
- Generic List - moving an item within the list
- generic NOT constraint where T IEnumerable
- Generics in C, using type of a variable as parameter
- Get a TextReader from a Stream?
- Get all column names of a DataTable into string array using LINQ/Predicate
- Get all derived types of a type

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.