Random.Next Method (Int32, Int32)
Returns a random number within a specified range.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- minValue
- Type: System.Int32
The inclusive lower bound of the random number returned.
- maxValue
- Type: System.Int32
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Return Value
Type: System.Int32A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException |
minValue is greater than maxValue. |
Unlike the other overloads of the Next method, which return only non-negative values, this method can return a negative random integer.
Notes to Inheritors
Starting with the .NET Framework version 2.0, if you derive a class from Random and override the Sample method, the distribution provided by the derived class implementation of the Sample method is not used in calls to the base class implementation of the Random.Next(Int32, Int32) method overload if the difference between the minValue and maxValue parameters is greater than Int32.MaxValue. Instead, the uniform distribution returned by the base Random class is used. This behavior improves the overall performance of the Random class. To modify this behavior to call the Sample method in the derived class, you must also override the Random.Next(Int32, Int32) method overload.
The following example uses the Random.Next(Int32, Int32) method to generate random integers with three distinct ranges. Note that the exact output from the example depends on the system-supplied seed value passed to the Random class constructor.
using System; public class Example { public static void Main() { Random rnd = new Random(); Console.WriteLine("\n20 random integers from -100 to 100:"); for (int ctr = 1; ctr <= 20; ctr++) { Console.Write("{0,6}", rnd.Next(-100, 101)); if (ctr % 5 == 0) Console.WriteLine(); } Console.WriteLine("\n20 random integers from 1000 to 10000:"); for (int ctr = 1; ctr <= 20; ctr++) { Console.Write("{0,8}", rnd.Next(1000, 10001)); if (ctr % 5 == 0) Console.WriteLine(); } Console.WriteLine("\n20 random integers from 1 to 10:"); for (int ctr = 1; ctr <= 20; ctr++) { Console.Write("{0,6}", rnd.Next(1, 11)); if (ctr % 5 == 0) Console.WriteLine(); } } } // The example displays output similar to the following: // 20 random integers from -100 to 100: // 65 -95 -10 90 -35 // -83 -16 -15 -19 41 // -67 -93 40 12 62 // -80 -95 67 -81 -21 // // 20 random integers from 1000 to 10000: // 4857 9897 4405 6606 1277 // 9238 9113 5151 8710 1187 // 2728 9746 1719 3837 3736 // 8191 6819 4923 2416 3028 // // 20 random integers from 1 to 10: // 9 8 5 9 9 // 9 1 2 3 8 // 1 4 8 10 5 // 9 7 9 10 5
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Ron,
I agree that you should avoid breaking changes, but why not just add a new method called "NextInRange" and, optionally, deprecate the old method?
A New Method?
I've passed your suggestion that Random include a new method whose second integer parameter is the inclusive upper bound of the integer range on to the feature team, which will consider it for possible inclusion in a future release of the .NET Framework. Thanks for the suggestion.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
- 1/25/2012
- DanThMan
- 2/7/2012
- R Petrusha - MSFT
The second parameter is named "MaxValue" which it clearly is not.
Suggest changing the implementation so that the second value is also inclusive.
Problems with Changing the Implementation
It is true that naming the second parameter maxValue and defining it as the exclusive upper bound of a range of random integers has caused more than a little confusion, and that it may have been a poor choice. Changing it at this point, however, is even more problemmatic. A major goal of the .NET Framework is to provide a stable class library that maintains a high level of compatibility across versions. There are now four major releases of the .NET Framework, and in each of them, the second parameter of the Random.Next(Int32, Int32) method is an integer that is one greater than the upper bound of the range of random integers. To make the change at this point would mean that every single method call to Random.Next in existing code will be broken, since every method call now can produce a random number one greater than the desired upper bound. Moreover, this breaking change is one that can easily go unnoticed in legacy code, since an out-of-bounds random number will only be generated sporadically.
--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation
- 3/25/2011
- mooreg
- 4/2/2011
- R Petrusha - MSFT