1 Byte[] k = new Byte[10];
2 System.Random r = new Random();
3
4 //用随机数填充指定字节数组的元素
5 r.NextBytes(k);
6
7 for (int i = 0; i < k.Length - 1; i++)
8 {
9 System.Console.WriteLine(k[i]);
10 }
11
12 //返回一个范围内的非负整随机数
13 for (int i = 0; i < 10; i++)
14 {
15 System.Console.WriteLine(r.Next(1, 3));
16 }
17
18 //返回一个介于 0.0 和 1.0 之间的随机数
19 for (int i = 0; i < 10; i++)
20 {
21 System.Console.WriteLine(r.NextDouble());
22 }
2 System.Random r = new Random();
3
4 //用随机数填充指定字节数组的元素
5 r.NextBytes(k);
6
7 for (int i = 0; i < k.Length - 1; i++)
8 {
9 System.Console.WriteLine(k[i]);
10 }
11
12 //返回一个范围内的非负整随机数
13 for (int i = 0; i < 10; i++)
14 {
15 System.Console.WriteLine(r.Next(1, 3));
16 }
17
18 //返回一个介于 0.0 和 1.0 之间的随机数
19 for (int i = 0; i < 10; i++)
20 {
21 System.Console.WriteLine(r.NextDouble());
22 }
我们来尝试做一个随机字符串生成
1 System.Random r = new Random();
2 string createStr = "";
3
4 do
5 {
6
7 for (int i = 0; i < 5; i++)
8 {
9 createStr +=
10 ((char)r.Next('A', 'Z' + 1)).ToString();
11 }
12 System.Console.WriteLine(createStr);
13 System.Console.WriteLine("要继续生成吗?(Y/N)");
14 }
15 while (System.Console.ReadLine() != "N");
2 string createStr = "";
3
4 do
5 {
6
7 for (int i = 0; i < 5; i++)
8 {
9 createStr +=
10 ((char)r.Next('A', 'Z' + 1)).ToString();
11 }
12 System.Console.WriteLine(createStr);
13 System.Console.WriteLine("要继续生成吗?(Y/N)");
14 }
15 while (System.Console.ReadLine() != "N");