Next Methode (Int32)
Inhaltsverzeichnis reduzieren
Inhaltsverzeichnis erweitern
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
Übersetzung
Original

Random.Next-Methode (Int32)

 

Gibt eine nicht negative Zufallsganzzahl zurück, die kleiner als das angegebene Maximum ist.

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

public virtual int Next(
	int maxValue
)

Parameter

maxValue

Die exklusive obere Grenze der Zufallszahl, die generiert werden soll. maxValue muss größer oder gleich 0 sein.

Rückgabewert

Type: System.Int32

Eine 32-Bit-Ganzzahl mit Vorzeichen, die größer oder gleich 0 und kleiner als maxValue ist, d. h. der Bereich der Rückgabewerte umfasst 0, aber nicht maxValue.Wenn jedoch maxValue 0 (null) entspricht, wird maxValue zurückgegeben.

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.
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
*/

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.

using System;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      string[] malePetNames = { "Rufus", "Bear", "Dakota", "Fido", 
                                "Vanya", "Samuel", "Koani", "Volodya", 
                                "Prince", "Yiska" };
      string[] femalePetNames = { "Maggie", "Penny", "Saya", "Princess", 
                                  "Abby", "Laila", "Sadie", "Olivia", 
                                  "Starlight", "Talla" };                                      

      // Generate random indexes for pet names.
      int mIndex = rnd.Next(malePetNames.Length);
      int fIndex = 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]);
   }
}
// The example displays the following output:
//       Suggested pet name of the day:
//          For a male:     Koani
//          For a female:   Maggie

Universal Windows Platform
Verfügbar seit 4.5
.NET Framework
Verfügbar seit 1.1
Portable Class Library
Unterstützt in: portable .NET platforms
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1
Zurück zum Anfang
Anzeigen:
© 2016 Microsoft