Random.NextBytes Method (Byte[])

 

Fills the elements of a specified array of bytes with random numbers.

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

abstract NextBytes : 
        buffer:byte[] -> unit
override NextBytes : 
        buffer:byte[] -> unit

Parameters

buffer
Type: System.Byte[]

An array of bytes to contain random numbers.

Exception Condition
ArgumentNullException

buffer is null.

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.

For example, to generate a cryptographically secured random number suitable for creating a random password, use a method such as RNGCryptoServiceProvider.GetBytes.

Notes to Inheritors:

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 method. Instead, the uniform distribution returned by the base Random class is used. This behavior improves the overall performance of the Random class. To modify this behavior to call the Sample method in the derived class, you must also override the NextBytes method.

The following example demonstrates how to use the NextBytes method to fill an array of bytes with random byte values.

No code example is currently available or this language may not be supported.

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: