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>)

Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs

Fills a span with cryptographically strong random bytes.

C#
public virtual void GetBytes(Span<byte> data);

Parameters

data
Span<Byte>

The span to fill with cryptographically strong random bytes.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

GetBytes(Byte[], Int32, Int32)

Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs

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

C#
public virtual void GetBytes(byte[] data, int offset, int count);

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

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetBytes(Byte[])

Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs

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

C#
public abstract void GetBytes(byte[] data);

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.

C#
byte[] random = new byte[100];

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
    rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.
}

Remarks

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

GetBytes(Int32)

Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs

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

C#
public static byte[] GetBytes(int count);

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

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10