double arraysum = 0.0; //定义一个生成随机数的数组 double[] array = new double[number]; //生成随机数方法 Random rdnumber = new Random(); // 生成N-1的随机数值 for (int i = 0; i < number - 1; i++) { array[i] = Math.Round(rdnumber.NextDouble() / number, 2); } //生成最后一位随机数;值为 1-上面随机数的和 for (int a = 0; a < array.Length; a++) { arraysum += array[a]; if (a == array.Length - 1) { array[a] = 1 - Math.Round(arraysum, 2); } } //随机数排序 Array.Sort(array);
--------------------------另一种方法,有规律的随机数 int n =5; double a =0; // var c =""; double[] value = new double[n]; double[] array = new double[n]; for (int i = 0; i < n ; i++) { value[i] = i + 1; a += value[i]; } for (int b = 0; b < n; b++) { array[b] = value[b] / a; // c += array[b] +","; } Array.Sort(array);