Random.NextBytes 方法

定义

重载

NextBytes(Byte[])

用随机数填充指定字节数组的元素。Fills the elements of a specified array of bytes with random numbers.

NextBytes(Span<Byte>)

用随机数填充指定字节范围的元素。Fills the elements of a specified span of bytes with random numbers.

NextBytes(Byte[])

用随机数填充指定字节数组的元素。Fills the elements of a specified array of bytes with random numbers.

public:
 virtual void NextBytes(cli::array <System::Byte> ^ buffer);
public virtual void NextBytes (byte[] buffer);
abstract member NextBytes : byte[] -> unit
override this.NextBytes : byte[] -> unit
Public Overridable Sub NextBytes (buffer As Byte())

参数

buffer
Byte[]

要用随机数填充的数组。The array to be filled with random numbers.

异常

buffernullbuffer is null.

示例

下面的示例演示如何使用 NextBytes 方法来使用随机字节值填充字节数组。The following example demonstrates how to use the NextBytes method to fill an array of bytes with random byte values.

#using <System.DLL>

using namespace System;

void main()
{
   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::WriteLine("{0}: {1}", i, b[i]);
}
// The example displays output similar to the following:
//       The Random bytes are:
//       0: 131
//       1: 96
//       2: 226
//       3: 213
//       4: 176
//       5: 208
//       6: 99
//       7: 89
//       8: 226
//       9: 194
Random rnd = new Random();
Byte[] b = new Byte[10];
rnd.NextBytes(b);
Console.WriteLine("The Random bytes are: ");
for (int i = 0; i <= b.GetUpperBound(0); i++) 
    Console.WriteLine("{0}: {1}", i, b[i]);  

// The example displays output similar to the following:
//       The Random bytes are:
//       0: 131
//       1: 96
//       2: 226
//       3: 213
//       4: 176
//       5: 208
//       6: 99
//       7: 89
//       8: 226
//       9: 194
Public Class Example
    Public Shared Sub Main()
        Dim rnd As New Random()
        Dim b(9) As Byte
        rnd.NextBytes(b)
        Console.WriteLine("The Random bytes are: ")
        For i As Integer = 0 To b.GetUpperBound(0)
            Console.WriteLine("{0}: {1}", i, b(i))
        Next
    End Sub 
End Class 
' The example displays output similar to the following:
'       The Random bytes are:
'       0: 131
'       1: 96
'       2: 226
'       3: 213
'       4: 176
'       5: 208
'       6: 99
'       7: 89
'       8: 226
'       9: 194

注解

字节数组的每个元素都设置为一个大于或等于0且小于或等于 MaxValue的随机数。Each element of the array of bytes is set to a random number greater than or equal to 0, and less than or equal to MaxValue.

例如,若要生成适用于创建随机密码的加密安全随机数字,请使用诸如 RNGCryptoServiceProvider.GetBytes的方法。For example, to generate a cryptographically secured random number suitable for creating a random password, use a method such as RNGCryptoServiceProvider.GetBytes.

继承者说明

从 .NET Framework 版本2.0 开始,如果从 Random 派生一个类并重写 Sample() 方法,则不会在对 NextBytes(Byte[]) 该方法的基类实现的调用中使用 Sample() 方法的派生类实现提供的分布。Starting with the .NET Framework version 2.0, if you derive a class from Random and override the Sample() method, the distribution provided by the derived class implementation of the Sample() method is not used in calls to the base class implementation of the NextBytes(Byte[]) method. 相反,将使用基 Random 类返回的统一分布。Instead, the uniform distribution returned by the base Random class is used. 此行为可提高 Random 类的整体性能。This behavior improves the overall performance of the Random class. 若要修改此行为以调用派生类中的 Sample() 方法,还必须重写 NextBytes(Byte[]) 方法。To modify this behavior to call the Sample() method in the derived class, you must also override the NextBytes(Byte[]) method.

另请参阅

NextBytes(Span<Byte>)

用随机数填充指定字节范围的元素。Fills the elements of a specified span of bytes with random numbers.

public:
 virtual void NextBytes(Span<System::Byte> buffer);
public virtual void NextBytes (Span<byte> buffer);
abstract member NextBytes : Span<byte> -> unit
override this.NextBytes : Span<byte> -> unit
Public Overridable Sub NextBytes (buffer As Span(Of Byte))

参数

buffer
Span<Byte>

要用随机数填充的数组。The array to be filled with random numbers.

注解

字节跨度的每个元素都设置为一个大于或等于0且小于或等于 MaxValue的随机数。Each element of the span of bytes is set to a random number greater than or equal to 0 and less than or equal to MaxValue.

适用于