C# Random Next()

Description

Random Next() returns a nonnegative random integer.

Syntax

Random.Next() has the following syntax.


public virtual int Next()

Returns

Random.Next() method returns A 32-bit signed integer greater than or equal to zero and less than MaxValue.

Example

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


// w  w  w  . j a  v  a2  s. co  m
using System;

public class RandomNextDemo  
{
    static void Main( )
    {  
        Random randObj = new Random( 1 );

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version