C# | Random.NextBytes() Method Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The NextBytes(Byte[]) method of the System.Random class in C# is used to fill the elements of a specified array of bytes with random numbers. This method takes a byte array as a parameter and fills it with random numbers. Syntax: public virtual void NextBytes (byte[] buffer); Here, buffer is the array of bytes to contain random numbers. Exception: This method will give ArgumentNullException if the buffer is null. Below programs illustrates the use of NextBytes() method: Example 1: csharp // C# program to illustrate the // use of Random.NextBytes Method using System; class GFG { // Driver code public static void Main() { // Instantiate random number generator Random rand = new Random(); // Instantiate an array of byte Byte[] b = new Byte[10]; rand.NextBytes(b); // Print random numbers in the byte array Console.WriteLine("Printing random numbers"+ " in the byte array"); for (int i = 0; i < 10; i++) Console.WriteLine("{0} -> {1}", i, b[i]); } } Output: Printing random numbers in the byte array 0 -> 63 1 -> 166 2 -> 5 3 -> 212 4 -> 114 5 -> 94 6 -> 161 7 -> 4 8 -> 226 9 -> 46 Example 2: csharp // C# program to illustrate the // use of Random.NextBytes Method using System; class GFG { // Driver code public static void Main() { // Instantiate random number generator Random rand = new Random(); // Instantiate an array of byte Byte[] b = new Byte[10]; rand.NextBytes(b); // Print random numbers in the byte array Console.WriteLine("Printing random numbers"+ " in the byte array"); foreach(byte byteValue in b) Console.WriteLine("{0}", byteValue); } } Output: Printing random numbers in the byte array 98 68 221 160 179 78 172 129 121 179 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Random.NextDouble() Method R rupesh_rao Follow Improve Article Tags : C# CSharp-method CSharp-Random-Class Similar Reads C# | Random.Next() Method The Next() Method of System.Random class in C# is used to get a random integer number. This method can be overloaded by passing different parameters to it as follows: Next() Next(Int32) Next(Int32, Int32) Next() Method This method is used to returns a non-negative random integer. Syntax: public virt 3 min read C# | Random.Next() Method The Next() Method of System.Random class in C# is used to get a random integer number. This method can be overloaded by passing different parameters to it as follows: Next() Next(Int32) Next(Int32, Int32) Next() Method This method is used to returns a non-negative random integer. Syntax: public virt 3 min read C# | Random.NextDouble() Method The NextDouble() Method of System.Random class in C# is used to return a random floating-point number which is greater than or equal to 0.0, and less than 1.0. Syntax: public virtual double NextDouble(); Return Value: This method returns a double-precision floating point number which is greater than 2 min read C# | Random.NextDouble() Method The NextDouble() Method of System.Random class in C# is used to return a random floating-point number which is greater than or equal to 0.0, and less than 1.0. Syntax: public virtual double NextDouble(); Return Value: This method returns a double-precision floating point number which is greater than 2 min read C# - Randomly Generating Strings In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. A string is the representation of the text. In this article, we will learn how to randomly generate strings and alphanumeric strings. So to do the task we use the 6 min read rand() in C The rand() function in the C programming language is used to generate pseudo-random numbers. It is used in C to generate random numbers in the range 0 to RAND_MAX. The rand() function is part of the standard C library <stdlib.h> so to use this function, we need to include the <stdlib.h> 3 min read Like