C# Random NextBytes

Description

Random NextBytes fills the elements of a specified array of bytes with random numbers.

Syntax

Random.NextBytes has the following syntax.


public virtual void NextBytes(
  byte[] buffer
)

Parameters

Random.NextBytes has the following parameters.

  • buffer - An array of bytes to contain random numbers.

Returns

Random.NextBytes method returns

Example

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


using System;/*from  w w w. ja  va 2 s. c o m*/

public class Example 
{
    public static void Main() 
    {
        Random rnd = new Random();
        Byte[] b = new Byte[10];
        rnd.NextBytes(b);
        for (int i = 0; i <= b.GetUpperBound(0); i++) 
            Console.WriteLine("{0}: {1}", i, b[i]);  
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version