Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Random Class
Random Methods
Next Method
 Next Method (Int32)
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Random..::.Next Method (Int32)

Updated: November 2007

Returns a nonnegative random number less than the specified maximum.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Visual Basic (Declaration)
Public Overridable Function Next ( _
    maxValue As Integer _
) As Integer
Visual Basic (Usage)
Dim instance As Random
Dim maxValue As Integer
Dim returnValue As Integer

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

Parameters

maxValue
Type: System..::.Int32

The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero.

Return Value

Type: System..::.Int32

A 32-bit signed integer greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned.

ExceptionCondition
ArgumentOutOfRangeException

maxValue is less than zero.

The following code example generates random integers with various overloads of the Next method.

Visual Basic
' Example of the Random.Next( ) methods.
Imports System
Imports Microsoft.VisualBasic

Module  RandomNextDemo

    ' Generate random numbers with no bounds specified.
    Sub NoBoundsRandoms( seed As Integer )

        Console.WriteLine( vbCrLf & _
            "Random object, seed = {0}, no bounds:", seed )
        Dim randObj As New Random( seed )

        ' Generate six random integers from 0 to int.MaxValue.
        Dim j As Integer
        For j = 0 To 5
            Console.Write( "{0,11} ", randObj.Next( ) )
        Next j
        Console.WriteLine( )
    End Sub 

    ' Generate random numbers with an upper bound specified.
    Sub UpperBoundRandoms( seed As Integer, upper As Integer )

        Console.WriteLine( vbCrLf & _
            "Random object, seed = {0}, upper bound = {1}:", _
            seed, upper )
        Dim randObj As New Random( seed )

        ' Generate six random integers from 0 to the upper bound.
        Dim j As Integer
        For j = 0 To 5
            Console.Write( "{0,11} ", randObj.Next( upper ) )
        Next j
        Console.WriteLine( )
    End Sub 

    ' Generate random numbers with both bounds specified.
    Sub BothBoundsRandoms( _
        seed As Integer, lower As Integer, upper As Integer )

        Console.WriteLine( vbCrLf & _
            "Random object, seed = {0}, lower = {1}, " & _
            "upper = {2}:", seed, lower, upper )
        Dim randObj As New Random( seed )

        ' Generate six random integers from the lower to 
        ' upper bounds.
        Dim j As Integer
        For j = 0 To 5
            Console.Write( "{0,11} ", _
                randObj.Next( lower, upper ) )
        Next j
        Console.WriteLine( )
    End Sub 

    Sub Main( )
        Console.WriteLine( _
            "This example of the Random.Next( ) methods" & _ 
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( _
            "Create Random objects all with the same seed " & _
            "and generate" & vbCrLf & "sequences of numbers " & _
            "with different bounds. Note the effect " & vbCrLf & _
            "that the various combinations " & _
            "of bounds have on the sequences." )

        NoBoundsRandoms( 234 )

        UpperBoundRandoms( 234, Int32.MaxValue )
        UpperBoundRandoms( 234, 2000000000 )
        UpperBoundRandoms( 234, 200000000 )

        BothBoundsRandoms( 234, 0, Int32.MaxValue )
        BothBoundsRandoms( 234, Int32.MinValue, Int32.MaxValue )
        BothBoundsRandoms( 234, -2000000000, 2000000000 )
        BothBoundsRandoms( 234, -200000000, 200000000 )
        BothBoundsRandoms( 234, -2000, 2000 )
    End Sub 
End Module 

' This example of the Random.Next( ) methods
' generates the following output.
' 
' Create Random objects all with the same seed and generate
' sequences of numbers with different bounds. Note the effect
' that the various combinations of bounds have on the sequences.
' 
' Random object, seed = 234, no bounds:
'  2091148258  1024955023   711273344  1081917183  1833298756   109460588
' 
' Random object, seed = 234, upper bound = 2147483647:
'  2091148258  1024955023   711273344  1081917183  1833298756   109460588
' 
' Random object, seed = 234, upper bound = 2000000000:
'  1947533580   954563751   662424922  1007613896  1707392518   101943116
' 
' Random object, seed = 234, upper bound = 200000000:
'   194753358    95456375    66242492   100761389   170739251    10194311
' 
' Random object, seed = 234, lower = 0, upper = 2147483647:
'  2091148258  1024955023   711273344  1081917183  1833298756   109460588
' 
' Random object, seed = 234, lower = -2147483648, upper = 2147483647:
'  2034812868   -97573602  -724936960    16350718  1519113864 -1928562472
' 
' Random object, seed = 234, lower = -2000000000, upper = 2000000000:
'  1895067160   -90872498  -675150156    15227793  1414785036 -1796113767
' 
' Random object, seed = 234, lower = -200000000, upper = 200000000:
'   189506716    -9087250   -67515016     1522779   141478503  -179611377
' 
' Random object, seed = 234, lower = -2000, upper = 2000:
'        1895         -91        -676          15        1414       -1797

C#
// Example of the Random.Next( ) methods.
using System;

public class RandomNextDemo  
{
    // Generate random numbers with no bounds specified.
    static void NoBoundsRandoms( int seed )
    {
        Console.WriteLine( 
            "\nRandom object, seed = {0}, no bounds:", seed );
        Random randObj = new Random( seed );

        // Generate six random integers from 0 to int.MaxValue.
        for( int j = 0; j < 6; j++ )
            Console.Write( "{0,11} ", randObj.Next( ) );
        Console.WriteLine( );
    }

    // Generate random numbers with an upper bound specified.
    static void UpperBoundRandoms( int seed, int upper )
    {
        Console.WriteLine( 
            "\nRandom object, seed = {0}, upper bound = {1}:", 
            seed, upper );
        Random randObj = new Random( seed );

        // Generate six random integers from 0 to the upper bound.
        for( int j = 0; j < 6; j++ )
            Console.Write( "{0,11} ", randObj.Next( upper ) );
        Console.WriteLine( );
    }

    // Generate random numbers with both bounds specified.
    static void BothBoundsRandoms( int seed, int lower, int upper )
    {
        Console.WriteLine( 
            "\nRandom object, seed = {0}, lower = {1}, " +
            "upper = {2}:", seed, lower, upper );
        Random randObj = new Random( seed );

        // Generate six random integers from the lower to 
        // upper bounds.
        for( int j = 0; j < 6; j++ )
            Console.Write( "{0,11} ", 
                randObj.Next( lower, upper) );
        Console.WriteLine( );
    }

    static void Main( )
    {    
        Console.WriteLine(                 
            "This example of the Random.Next( ) methods\n" +
            "generates the following output.\n" );
        Console.WriteLine(                 
            "Create Random objects all with the same seed and " +
            "generate\nsequences of numbers with different " +
            "bounds. Note the effect\nthat the various " +
            "combinations of bounds have on the sequences." );
        
        NoBoundsRandoms( 234 );

        UpperBoundRandoms( 234, Int32.MaxValue );
        UpperBoundRandoms( 234, 2000000000 );
        UpperBoundRandoms( 234, 200000000 );

        BothBoundsRandoms( 234, 0, Int32.MaxValue );
        BothBoundsRandoms( 234, Int32.MinValue, Int32.MaxValue );
        BothBoundsRandoms( 234, -2000000000, 2000000000 );
        BothBoundsRandoms( 234, -200000000, 200000000 );
        BothBoundsRandoms( 234, -2000, 2000 );
    }
}

/*
This example of the Random.Next( ) methods
generates the following output.

Create Random objects all with the same seed and generate
sequences of numbers with different bounds. Note the effect
that the various combinations of bounds have on the sequences.

Random object, seed = 234, no bounds:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, upper bound = 2147483647:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, upper bound = 2000000000:
 1947533580   954563751   662424922  1007613896  1707392518   101943116

Random object, seed = 234, upper bound = 200000000:
  194753358    95456375    66242492   100761389   170739251    10194311

Random object, seed = 234, lower = 0, upper = 2147483647:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, lower = -2147483648, upper = 2147483647:
 2034812868   -97573602  -724936960    16350718  1519113864 -1928562472

Random object, seed = 234, lower = -2000000000, upper = 2000000000:
 1895067160   -90872498  -675150156    15227793  1414785036 -1796113767

Random object, seed = 234, lower = -200000000, upper = 200000000:
  189506716    -9087250   -67515016     1522779   141478503  -179611377

Random object, seed = 234, lower = -2000, upper = 2000:
       1895         -91        -676          15        1414       -1797
*/

Visual C++
// Example of the Random::Next( ) methods.
using namespace System;

// Generate random numbers with no bounds specified.
void NoBoundsRandoms( int seed )
{
   Console::WriteLine( "\nRandom object, seed = {0}, no bounds:", seed );
   Random^ randObj = gcnew Random( seed );

   // Generate six random integers from 0 to int.MaxValue.
   for ( int j = 0; j < 6; j++ )
      Console::Write(  "{0,11} ", randObj->Next() );
   Console::WriteLine();
}


// Generate random numbers with an upper bound specified.
void UpperBoundRandoms( int seed, int upper )
{
   Console::WriteLine( "\nRandom object, seed = {0}, upper bound = {1}:", seed, upper );
   Random^ randObj = gcnew Random( seed );

   // Generate six random integers from 0 to the upper bound.
   for ( int j = 0; j < 6; j++ )
      Console::Write(  "{0,11} ", randObj->Next( upper ) );
   Console::WriteLine();
}


// Generate random numbers with both bounds specified.
void BothBoundsRandoms( int seed, int lower, int upper )
{
   Console::WriteLine( "\nRandom object, seed = {0}, lower = {1}, upper = {2}:", seed, lower, upper );
   Random^ randObj = gcnew Random( seed );

   // Generate six random integers from the lower to 
   // upper bounds.
   for ( int j = 0; j < 6; j++ )
      Console::Write(  "{0,11} ", randObj->Next( lower, upper ) );
   Console::WriteLine();
}

int main()
{
   Console::WriteLine( "This example of the Random::Next( ) methods\n"
   "generates the following output.\n" );
   Console::WriteLine( "Create Random objects all with the same seed and "
   "generate\nsequences of numbers with different "
   "bounds. Note the effect\nthat the various "
   "combinations of bounds have on the sequences." );
   NoBoundsRandoms( 234 );
   UpperBoundRandoms( 234, Int32::MaxValue );
   UpperBoundRandoms( 234, 2000000000 );
   UpperBoundRandoms( 234, 200000000 );
   BothBoundsRandoms( 234, 0, Int32::MaxValue );
   BothBoundsRandoms( 234, Int32::MinValue, Int32::MaxValue );
   BothBoundsRandoms( 234, -2000000000, 2000000000 );
   BothBoundsRandoms( 234, -200000000, 200000000 );
   BothBoundsRandoms( 234, -2000, 2000 );
}

/*
This example of the Random::Next( ) methods
generates the following output.

Create Random objects all with the same seed and generate
sequences of numbers with different bounds. Note the effect
that the various combinations of bounds have on the sequences.

Random object, seed = 234, no bounds:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, upper bound = 2147483647:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, upper bound = 2000000000:
 1947533580   954563751   662424922  1007613896  1707392518   101943116

Random object, seed = 234, upper bound = 200000000:
  194753358    95456375    66242492   100761389   170739251    10194311

Random object, seed = 234, lower = 0, upper = 2147483647:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, lower = -2147483648, upper = 2147483647:
 2034812868   -97573602  -724936960    16350718  1519113864 -1928562472

Random object, seed = 234, lower = -2000000000, upper = 2000000000:
 1895067160   -90872498  -675150156    15227793  1414785036 -1796113767

Random object, seed = 234, lower = -200000000, upper = 200000000:
  189506716    -9087250   -67515016     1522779   141478503  -179611377

Random object, seed = 234, lower = -2000, upper = 2000:
       1895         -91        -676          15        1414       -1797
*/

J#
// Example of the Random.Next( ) methods.
import System.*;

public class RandomNextDemo
{
    // Generate random numbers with no bounds specified.
    static void NoBoundsRandoms(int seed)
    {
        Console.WriteLine("\nRandom object, seed = {0}, no bounds:",
            System.Convert.ToString(seed));
        Random randObj = new Random(seed);

        // Generate six random integers from 0 to int.MaxValue.
        for (int j = 0; j < 6; j++) {
            Console.Write("{0,11} ", System.Convert.ToString(randObj.Next()));
        }
        Console.WriteLine();
    } //NoBoundsRandoms

    // Generate random numbers with an upper bound specified.
    static void UpperBoundRandoms(int seed, int upper)
    {
        Console.WriteLine("\nRandom object, seed = {0}, upper bound = {1}:", 
            System.Convert.ToString(seed), System.Convert.ToString(upper));
        Random randObj = new Random(seed);

        // Generate six random integers from 0 to the upper bound.
        for (int j = 0; j < 6; j++) {
            Console.Write("{0,11} ",
                System.Convert.ToString(randObj.Next(upper)));
        }
        Console.WriteLine();
    } //UpperBoundRandoms

    // Generate random numbers with both bounds specified.
    static void BothBoundsRandoms(int seed, int lower, int upper)
    {
        Console.WriteLine("\nRandom object, seed = {0}, lower = {1}, "
            + "upper = {2}:", System.Convert.ToString(seed),
            System.Convert.ToString(lower), System.Convert.ToString(upper));
        Random randObj = new Random(seed);
        // Generate six random integers from the lower to 
        // upper bounds.
        for (int j = 0; j < 6; j++) {
            Console.Write("{0,11} ",
                System.Convert.ToString(randObj.Next(lower, upper)));
        }
        Console.WriteLine();
    } //BothBoundsRandoms

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the Random.Next( ) methods\n" 
            + "generates the following output.\n"));
        Console.WriteLine(("Create Random objects all with the same seed and "
            + "generate\nsequences of numbers with different "
            + "bounds. Note the effect\nthat the various " 
            + "combinations of bounds have on the sequences."));
        NoBoundsRandoms(234);
        UpperBoundRandoms(234, Int32.MaxValue);
        UpperBoundRandoms(234, 2000000000);
        UpperBoundRandoms(234, 200000000);
        BothBoundsRandoms(234, 0, Int32.MaxValue);
        BothBoundsRandoms(234, Int32.MinValue, Int32.MaxValue);
        BothBoundsRandoms(234, -2000000000, 2000000000);
        BothBoundsRandoms(234, -200000000, 200000000);
        BothBoundsRandoms(234, -2000, 2000);
    } //main
} //RandomNextDemo

/*
This example of the Random.Next( ) methods
generates the following output.

Create Random objects all with the same seed and generate
sequences of numbers with different bounds. Note the effect
that the various combinations of bounds have on the sequences.

Random object, seed = 234, no bounds:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, upper bound = 2147483647:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, upper bound = 2000000000:
 1947533580   954563751   662424922  1007613896  1707392518   101943116

Random object, seed = 234, upper bound = 200000000:
  194753358    95456375    66242492   100761389   170739251    10194311

Random object, seed = 234, lower = 0, upper = 2147483647:
 2091148258  1024955023   711273344  1081917183  1833298756   109460588

Random object, seed = 234, lower = -2147483648, upper = 2147483647:
 2034812868   -97573602  -724936960    16350718  1519113864 -1928562472

Random object, seed = 234, lower = -2000000000, upper = 2000000000:
 1895067160   -90872498  -675150156    15227793  1414785036 -1796113767

Random object, seed = 234, lower = -200000000, upper = 200000000:
  189506716    -9087250   -67515016     1522779   141478503  -179611377

Random object, seed = 234, lower = -2000, upper = 2000:
       1895         -91        -676          15        1414       -1797
*/

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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

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: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker