Next Method (Int32, Int32)
.NET Framework Class Library
Random..::.Next Method (Int32, Int32)

Updated: February 2009

Returns a random number within a specified range.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overridable Function Next ( _
    minValue As Integer, _
    maxValue As Integer _
) As Integer
Visual Basic (Usage)
Dim instance As Random
Dim minValue As Integer
Dim maxValue As Integer
Dim returnValue As Integer

returnValue = instance.Next(minValue, _
    maxValue)
C#
public virtual int Next(
    int minValue,
    int maxValue
)
Visual C++
public:
virtual int Next(
    int minValue, 
    int maxValue
)
JScript
public function Next(
    minValue : int, 
    maxValue : int
) : int

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..::.Int32
A 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.
ExceptionCondition
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.

Visual Basic
Module Example
   Public Sub Main()
      Dim rnd As New Random()

      Console.WriteLine("20 random integers from -100 to 100:")
      For ctr As Integer = 1 To 20
         Console.Write("{0,6}", rnd.Next(-100, 101))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next
      Console.WriteLine()

      Console.WriteLine("20 random integers from 1000 to 10000:")      
      For ctr As Integer = 1 To 20
         Console.Write("{0,8}", rnd.Next(1000, 10001))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next
      Console.WriteLine()

      Console.WriteLine("20 random integers from 1 to 10:")
      For ctr As Integer = 1 To 20
         Console.Write("{0,6}", rnd.Next(1, 11))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next
   End Sub
End Module
' 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
C#
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, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

February 2009

Replaced the example.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Misleading 'maxValue' parameter name      ieatskunk   |   Edit   |   Show History
So why is it that the maxValue parameter is exclusive whilst the minValue is inclusive? This doesn't make sense. The max value should be the maximum value you would want returned but instead it is the minimum value of all values greater than the range you want returned. No matter how you look at it the maxValue parameter is not the maxValue of anything.

Tags What's this?: Add a tag
Flag as ContentBug
Random numbers not distinct      Sudhakar Naga   |   Edit   |   Show History
When I try to generate 3 random integers between 1 & 5 (I gave maxValue as 6), atleast 50% of the time I don't get distinct random numbers. I want to generate 3 random integers from a set of 5 integers then why am I getting repetead values?
Flag as ContentBug
Processing
Page view tracker