RandomNumberGenerator.GetNonZeroBytes Method

Definition

Overloads

GetNonZeroBytes(Byte[])

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

GetNonZeroBytes(Span<Byte>)

Fills a byte span with a cryptographically strong random sequence of nonzero values.

GetNonZeroBytes(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 nonzero values.

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

Parameters

data
Byte[]

The array to fill with cryptographically strong random nonzero bytes.

Examples

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

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

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
    rng.GetNonZeroBytes(random); // The array is now filled with cryptographically strong random bytes, and none are zero.
}

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 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 2.0, 2.1

GetNonZeroBytes(Span<Byte>)

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

Fills a byte span with a cryptographically strong random sequence of nonzero values.

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

Parameters

data
Span<Byte>

The span to fill with cryptographically strong random nonzero bytes.

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