NextBytes Method
.NET Framework Class Library
Random..::.NextBytes Method

Fills the elements of a specified array of bytes with random numbers.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overridable Sub NextBytes ( _
    buffer As Byte() _
)
Visual Basic (Usage)
Dim instance As Random
Dim buffer As Byte()

instance.NextBytes(buffer)
C#
public virtual void NextBytes(
    byte[] buffer
)
Visual C++
public:
virtual void NextBytes(
    array<unsigned char>^ buffer
)
JScript
public function NextBytes(
    buffer : byte[]
)

Parameters

buffer
Type: array<System..::.Byte>[]()[]
An array of bytes to contain random numbers.
ExceptionCondition
ArgumentNullException

buffer is nullNothingnullptra null reference (Nothing in Visual Basic).

Each element of the array of bytes is set to a random number greater than or equal to zero, and less than or equal to MaxValue.

To generate a cryptographically secured random number suitable for creating a random password, for example, use a method such as RNGCryptoServiceProvider..::.GetBytes.

Notes to Inheritors:

Starting with the .NET Framework version 2.0, if you derive a class from Random and override the Sample method, the distribution provided by the derived class implementation of the Sample method is not used in calls to the base class implementation of the NextBytes method. Instead, the uniform distribution returned by the base Random class is used. This behavior improves the overall performance of the Random class. To modify this behavior to call the Sample method in the derived class, you must also override the NextBytes method.

The following code example demonstrates how to use the NextBytes method to fill an array of bytes with random byte values.

Visual Basic
Dim rnd As New Random()
Dim b(10) As Byte
rnd.NextBytes(b)
Console.WriteLine("The Random bytes are: ")
Dim i As Integer
For i = 0 To 9
    Console.Write(i)
    Console.Write(":")
    Console.WriteLine(b(i))
Next i
C#
Random rnd = new Random();
Byte[] b = new Byte[10];
rnd.NextBytes(b);
Console.WriteLine("The Random bytes are: ");
for (int i = 0; i < 10; i++) {
    Console.Write(i);  
    Console.Write(":");
    Console.WriteLine(b[i]);
}
Visual C++
#using <System.DLL>

using namespace System;
int main()
{

   Random^ rnd = gcnew Random;
   array<unsigned char>^b = gcnew array<unsigned char>(10);
   rnd->NextBytes( b );
   Console::WriteLine( "The Random bytes are: " );
   for ( int i = 0; i < 10; i++ )
   {
      Console::Write( i );
      Console::Write( ":" );
      Console::WriteLine( b[ i ] );

   }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Can buffer contain twice (or more) the same random number?      rudimenter ... R Petrusha - MSFT   |   Edit   |   Show History
Is it possible that the Buffer Array contains the same number more then once?

Duplicate Random Numbers

Yes, buffer can include bytes that have the same value. The probability that two contiguous bytes will have the same value is 1/256, or slightly less than four times per thousand.

Imposing a requirement that a number be unique would be problematic in two ways. First, it would necessarily limit the maximum size of the array to 256 elements, since the range of the Byte structure is 0-255. Second, it's debatable whether such a number could be considered random.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker