Export (0) Print
Expand All

RandomNumberGenerator.GetNonZeroBytes Method

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

[Visual Basic]
Public MustOverride Sub GetNonZeroBytes( _
   ByVal data() As Byte _
)
[C#]
public abstract void GetNonZeroBytes(
 byte[] data
);
[C++]
public: virtual void GetNonZeroBytes(
 unsigned char data __gc[]
) = 0;
[JScript]
public abstract function GetNonZeroBytes(
   data : Byte[]
);

Parameters

data
The array to fill with cryptographically strong random nonzero bytes.

Remarks

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

Example

[Visual Basic, C#, C++] The following example creates a random sequence of 100 nonzero bytes and stores it in random.

[Visual Basic] 
Dim random() As Byte = New Byte(100) {}
'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetNonZeroBytes(random) ' bytes in random are now random and none are zero

[C#] 
byte[] random = new Byte[100];
//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(random); // The array is now filled with cryptographically strong random bytes, and none are zero.

[C++] 
Byte random[] = new Byte[100];
//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider* rng = new RNGCryptoServiceProvider();
rng->GetNonZeroBytes(random); // The array is now filled with cryptographically strong random bytes, and none are zero.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RandomNumberGenerator Class | RandomNumberGenerator Members | System.Security.Cryptography Namespace | Cryptographic Services

Show:
© 2014 Microsoft