Random.Next Method (Int32)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns a non-negative random integer that is less than the specified maximum.

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

Public Overridable Function Next (
	maxValue As Integer
) As Integer

Parameters

maxValue
Type: System.Int32

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

Return Value

Type: System.Int32

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

Exception Condition
ArgumentOutOfRangeException

maxValue is less than 0.

The Next(Int32) overload returns random integers that range from 0 to maxValue – 1. However, if maxValue is 0, the method returns 0.

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

' 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

The following example generates a random integer that it uses as an index to retrieve a string value from an array. Because the highest index of the array is one less than its length, the value of the Array.Length property is supplied as a the maxValue parameter.

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim malePetNames() As String = { "Rufus", "Bear", "Dakota", "Fido", 
                                    "Vanya", "Samuel", "Koani", "Volodya", 
                                    "Prince", "Yiska" }
      Dim femalePetNames() As String = { "Maggie", "Penny", "Saya", "Princess", 
                                         "Abby", "Laila", "Sadie", "Olivia", 
                                         "Starlight", "Talla" }                                      

      ' Generate random indexes for pet names.
      Dim mIndex As Integer = rnd.Next(malePetNames.Length)
      Dim fIndex As Integer = rnd.Next(femalePetNames.Length)

      ' Display the result.
      Console.WriteLine("Suggested pet name of the day: ")
      Console.WriteLine("   For a male:     {0}", malePetNames(mIndex))
      Console.WriteLine("   For a female:   {0}", femalePetNames(fIndex))
   End Sub
End Module
' The example displays output like the following:
'       Suggested pet name of the day:
'          For a male:     Koani
'          For a female:   Maggie

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: