C#中的Random.NextBytes()方法
C#中的Random.NextBytes()方法用于用随机数填充指定字节数组的元素。
语法
语法如下-
public virtual void NextBytes (byte[] buffer);
缓冲区上方是字节数组。
示例
现在让我们看一个例子-
using System;
public class Demo {
public static void Main(){
Random r = new Random();
Random r2 = new Random();
Random r3 = new Random();
Byte[] arr = new Byte[5];
r3.NextBytes(arr);
Console.WriteLine("Random numbers.....");
for (int i = 1; i <= 5; i++)
Console.WriteLine(r.Next());
Console.WriteLine("\nRandom numbers from 1 to 10.....");
for (int i = 1; i <= 5; i++)
Console.WriteLine(r2.Next(10));
Console.WriteLine("\nRandom numbers in the byte array...");
for (int i = 0; i < arr.GetUpperBound(0); i++)
Console.WriteLine(arr[i]);
}
}输出结果
这将产生以下输出-
Random numbers..... 2081486546 329484380 1639318640 1499756340 2122408387 Random numbers from 1 to 10..... 9 1 7 6 9 Random numbers in the byte array... 210 92 112 52
示例
现在让我们来看另一个示例-
using System;
public class Demo {
public static void Main(){
Random r = new Random();
Byte[] arr = new Byte[2];
r.NextBytes(arr);
Console.WriteLine("Random numbers in the byte array...");
for (int i = 0; i < 2; i++)
Console.WriteLine(arr[i]);
}
}输出结果
这将产生以下输出-
Random numbers in the byte array... 173 11
热门推荐
10 爱情贺卡祝福语简短英语
11 娃娃考试祝福语幽默简短
12 结婚祝福语简短精辟 兄弟
13 表达朋友的祝福语简短
14 春节感恩祝福语简短的
15 结婚祝福语粤语大全简短
16 恭喜朋友升职祝福语简短
17 圣诞饭店祝福语大全简短
18 建工系毕业祝福语简短