c#分割随机数

static string[] results = new string[7];

        static void Main(string[] args)
        {
            Console.WriteLine("请输入总数:");
            int total = int.Parse(Console.ReadLine());
            Console.WriteLine("请指定要分成几份:");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine("请指定最大浮动值:");
            int maxFloatNumber = int.Parse(Console.ReadLine());
            SplitTheNumber(total, n, maxFloatNumber);
        }

        private static void SplitTheNumber(int total, int n, int maxFloatNumber)
        {
            Random r = new Random();
            int baseNumber = total / n; //获取基数
            int result = 0; //用来存储结果值
            for (int i = 1; i <=n-1; i++)
            {
                result = r.Next(baseNumber - maxFloatNumber, baseNumber + maxFloatNumber); //在浮动范围内取一个随机数
                total = total - result; //从总数中减掉结果值
                results[i - 1] = result.ToString(); //得到的结果值写入数组
                Console.WriteLine(result);
            }
            results[6] = total.ToString(); //最后剩下的值写入数组
            Console.WriteLine(total);
            File.WriteAllLines("1.txt", results);
            Console.ReadKey();
        }

你可能感兴趣的:(c#分割随机数)