C#中的Random.NextDouble()方法
C#中的Random.NextDouble()方法用于返回一个大于或等于0.0且小于1.0的随机浮点数。
语法
语法如下-
public virtual double NextDouble ();
示例
现在让我们看一个例子-
using System;
public class Demo {
public static void Main(){
Random r1 = new Random();
Random r2 = new Random();
Byte[] arr = new Byte[2];
r1.NextBytes(arr);
Console.WriteLine("Random numbers in the byte array...");
for (int i = 0; i < 2; i++)
Console.WriteLine(arr[i]);
Console.WriteLine("\nRandom floating point numbers...");
for (int i = 0; i < 5; i++)
Console.WriteLine(r2.NextDouble());
}
}输出结果
这将产生以下输出-
Random numbers in the byte array... 124 141 Random floating point numbers... 0.93591266727816 0.36406785872023 0.122396959514542 0.795166163144245 0.954394097884369
示例
现在让我们来看另一个示例-
using System;
public class Demo {
public static void Main(){
int[] val = new int[7];
Random r = new Random();
double d;
for (int i = 0; i 50; i++) {
d = r.NextDouble();
val[(int) Math.Ceiling(d*5)] ++;
}
Console.WriteLine("Random Numbers...");
for (int i = 0; i < 7; i++)
Console.WriteLine(val[i]);
}
}输出结果
这将产生以下输出-
Random Numbers... 0 13 9 12 8 8 0
热门推荐
2 万寿祝福语简短
7 足球队祝福语简短
10 中秋快乐祝福语简短的
11 周末祝福语正能量简短
12 元旦联谊祝福语简短精辟
13 孙女结婚姥姥祝福语简短
14 简短对公司的祝福语
15 春节后的祝福语简短
16 圆圆的祝福语简短
17 年月日祝福语简短
18 送外卖祝福语大全简短