生成随机数可以使用System.Random
类。以下是生成随机数的几种常见场景和示例代码。
1. 生成一个随机整数
生成一个指定范围内的随机整数。
using System;class Program
{static void Main(string[] args){Random random = new Random();int randomNumber = random.Next(1, 101); // 生成1到100之间的随机整数Console.WriteLine("随机整数: " + randomNumber);}
}
说明:
-
random.Next(minValue, maxValue)
:生成[minValue, maxValue)
范围内的随机整数。 -
示例中生成1到100之间的随机整数。
2. 生成一个随机浮点数
生成一个0到1之间的随机浮点数。
using System;class Program
{static void Main(string[] args){Random random = new Random();double randomDouble = random.NextDouble(); // 生成0到1之间的随机浮点数Console.WriteLine("随机浮点数: " + randomDouble);}
}
说明:
-
random.NextDouble()
:生成[0.0, 1.0)
范围内的随机浮点数。
3. 生成一个指定范围的随机浮点数
生成一个指定范围内的随机浮点数。
using System;class Program
{static void Main(string[] args){Random random = new Random();double min = 10.5;double max = 20.5;double randomDouble = min + (random.NextDouble() * (max - min)); // 生成10.5到20.5之间的随机浮点数Console.WriteLine("随机浮点数: " + randomDouble);}
}
说明:
-
通过公式
min + (random.NextDouble() * (max - min))
,可以生成[min, max)
范围内的随机浮点数。
4. 生成一个随机字符串
生成一个指定长度的随机字符串。
using System;
using System.Text;class Program
{static void Main(string[] args){string randomString = GenerateRandomString(10); // 生成长度为10的随机字符串Console.WriteLine("随机字符串: " + randomString);}static string GenerateRandomString(int length){const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";Random random = new Random();StringBuilder sb = new StringBuilder(length);for (int i = 0; i < length; i++){int index = random.Next(chars.Length);sb.Append(chars[index]);}return sb.ToString();}
}
说明:
-
使用
Random
类从字符集合中随机选择字符,生成指定长度的随机字符串。
5. 生成一个随机布尔值
生成一个随机的布尔值(true
或false
)。
using System;class Program
{static void Main(string[] args){Random random = new Random();bool randomBool = random.Next(2) == 0; // 生成随机的布尔值Console.WriteLine("随机布尔值: " + randomBool);}
}
说明:
-
random.Next(2)
生成0或1,然后将其转换为布尔值。
6. 生成一个随机数组
生成一个包含随机整数的数组。
using System;class Program
{static void Main(string[] args){int[] randomArray = GenerateRandomArray(5, 1, 100); // 生成长度为5的随机数组,范围1到100Console.WriteLine("随机数组: " + string.Join(", ", randomArray));}static int[] GenerateRandomArray(int length, int minValue, int maxValue){Random random = new Random();int[] array = new int[length];for (int i = 0; i < length; i++){array[i] = random.Next(minValue, maxValue);}return array;}
}
说明:
-
生成一个指定长度和范围的随机整数数组。
7. 生成不重复的随机数
生成一组不重复的随机数。
using System;
using System.Collections.Generic;class Program
{static void Main(string[] args){List<int> randomNumbers = GenerateUniqueRandomNumbers(1, 100, 10); // 生成10个1到100之间的不重复随机数Console.WriteLine("不重复随机数: " + string.Join(", ", randomNumbers));}static List<int> GenerateUniqueRandomNumbers(int minValue, int maxValue, int count){if (count > (maxValue - minValue + 1)){throw new ArgumentException("范围不足以生成指定数量的不重复随机数。");}Random random = new Random();HashSet<int> numbers = new HashSet<int>();while (numbers.Count < count){numbers.Add(random.Next(minValue, maxValue + 1));}return new List<int>(numbers);}
}
说明:
-
使用
HashSet
确保生成的随机数不重复。 -
如果范围不足以生成指定数量的不重复随机数,会抛出异常。
8. 使用种子生成随机数
通过指定种子生成可重复的随机数序列。
using System;class Program
{static void Main(string[] args){int seed = 12345; // 种子值Random random = new Random(seed);for (int i = 0; i < 5; i++){Console.WriteLine("随机数: " + random.Next(1, 101));}}
}
说明:
-
使用相同的种子会生成相同的随机数序列,适合需要可重复随机数的场景。