Freigeben über


Random.NextBytes-Methode

Füllt die Elemente eines angegebenen Bytearrays mit Zufallszahlen.

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

Syntax

'Declaration
Public Overridable Sub NextBytes ( _
    buffer As Byte() _
)
'Usage
Dim instance As Random
Dim buffer As Byte()

instance.NextBytes(buffer)
public virtual void NextBytes (
    byte[] buffer
)
public:
virtual void NextBytes (
    array<unsigned char>^ buffer
)
public void NextBytes (
    byte[] buffer
)
public function NextBytes (
    buffer : byte[]
)

Parameter

  • buffer
    Ein Bytearray, das für Zufallszahlen vorgesehen ist.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

buffer ist NULL (Nothing in Visual Basic).

Hinweise

Jedes Element des Bytearrays wird auf eine Zufallszahl festgelegt. Diese ist größer oder gleich null und kleiner oder gleich MaxValue.

Verwenden Sie zum Generieren einer kryptografisch sicheren Zufallszahl, z. B. für das Erstellen eines zufälligen Kennworts, eine Methode wie RNGCryptoServiceProvider.GetBytes.

Beispiel

Dim rnd As New Random()
Dim b(10) As Byte
rnd.NextBytes(b)
Console.WriteLine("The Random bytes are: ")
Dim i As Integer
For i = 0 To 9
    Console.Write(i)
    Console.Write(":")
    Console.WriteLine(b(i))
Next i
Random rnd = new Random();
Byte[] b = new Byte[10];
rnd.NextBytes(b);
Console.WriteLine("The Random bytes are: ");
for (int i = 0; i < 10; i++) {
    Console.Write(i);  
    Console.Write(":");
    Console.WriteLine(b[i]);
}
   Random^ rnd = gcnew Random;
   array<unsigned char>^b = gcnew array<unsigned char>(10);
   rnd->NextBytes( b );
   Console::WriteLine( "The Random bytes are: " );
   for ( int i = 0; i < 10; i++ )
   {
      Console::Write( i );
      Console::Write( ":" );
      Console::WriteLine( b[ i ] );

   }
}

/*
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/
Random rnd = new Random();
ubyte b[] = new ubyte[10];
rnd.NextBytes(b);
Console.WriteLine("The Random bytes are: ");
for (int i = 0; i < 10; i++) {
    Console.Write(i);
    Console.Write(":");
    Console.WriteLine(b[i]);
}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Random-Klasse
Random-Member
System-Namespace
Byte-Struktur