RandomNumberGenerator.GetBytes Method

Definition

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

Overloads

GetBytes(Span<Byte>)

Fills a span with cryptographically strong random bytes.

GetBytes(Byte[], Int32, Int32)

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

GetBytes(Byte[])

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

GetBytes(Int32)

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

GetBytes(Span<Byte>)

Fills a span with cryptographically strong random bytes.

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>

The span to fill with cryptographically strong random bytes.

See also

Applies to

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

The index of the array to start the fill operation.

count
Int32

The number of bytes to fill.

Exceptions

data is null.

offset or count is less than 0

offset plus count exceeds the length of data.

Applies to

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.

See also

Applies to

GetBytes(Int32)

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

public:
 static cli::array <System::Byte> ^ GetBytes(int count);
public static byte[] GetBytes (int count);
static member GetBytes : int -> byte[]
Public Shared Function GetBytes (count As Integer) As Byte()

Parameters

count
Int32

The number of bytes of random values to create.

Returns

Byte[]

An array populated with cryptographically strong random values.

Exceptions

count is less than zero.

Applies to