RandomNumberGenerator.GetBytes RandomNumberGenerator.GetBytes RandomNumberGenerator.GetBytes RandomNumberGenerator.GetBytes Method

Definition

Fills an array of bytes with a cryptographically strong random sequence of values.

Overloads

GetBytes(Byte[]) GetBytes(Byte[]) GetBytes(Byte[]) GetBytes(Byte[])

When overridden in a derived class, fills an array of bytes with a cryptographically strong random sequence of values.

GetBytes(Span<Byte>) GetBytes(Span<Byte>) GetBytes(Span<Byte>) GetBytes(Span<Byte>)
GetBytes(Byte[], Int32, Int32) GetBytes(Byte[], Int32, Int32) GetBytes(Byte[], Int32, Int32) GetBytes(Byte[], Int32, Int32)

Fills the specified byte array with a cryptographically strong random sequence of values.

GetBytes(Byte[]) GetBytes(Byte[]) GetBytes(Byte[]) GetBytes(Byte[])

When overridden in a derived class, fills an array of bytes with a cryptographically strong random sequence of values.

public:
 abstract void GetBytes(cli::array <System::Byte> ^ data);
public abstract void GetBytes (byte[] data);
abstract member GetBytes : byte[] -> unit
Public MustOverride Sub GetBytes (data As Byte())
Parameters
data
Byte[]

The array to fill with cryptographically strong random bytes.

Examples

The following example creates a random sequence 100 bytes long and stores it in random.

array<Byte>^ random = gcnew array<Byte>(100);

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;
rng->GetBytes( random ); // The array is now filled with cryptographically strong random bytes.
byte[] random = new Byte[100];

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.
Dim random() As Byte = New Byte(100) {}
       
'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetBytes(random) ' bytes in random are now random

Remarks

The length of the byte array determines how many random bytes are produced.

GetBytes(Span<Byte>) GetBytes(Span<Byte>) GetBytes(Span<Byte>) GetBytes(Span<Byte>)

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

GetBytes(Byte[], Int32, Int32) GetBytes(Byte[], Int32, Int32) GetBytes(Byte[], Int32, Int32) GetBytes(Byte[], Int32, Int32)

Fills the specified byte array with a cryptographically strong random sequence of values.

public:
 virtual void GetBytes(cli::array <System::Byte> ^ data, int offset, int count);
public virtual void GetBytes (byte[] data, int offset, int count);
abstract member GetBytes : byte[] * int * int -> unit
override this.GetBytes : byte[] * int * int -> unit
Public Overridable Sub GetBytes (data As Byte(), offset As Integer, count As Integer)
Parameters
data
Byte[]

The array to fill with cryptographically strong random bytes.

offset
Int32 Int32 Int32 Int32

The index of the array to start the fill operation.

count
Int32 Int32 Int32 Int32

The number of bytes to fill.

Exceptions

offset plus count exceeds the length of data.

Applies to