110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int total = 3000;
int minrange4 = 2000;
int maxrange4 = 4000;
int count = 3;
List<int> result = foo(total, minrange4, maxrange4, count);
foreach (var item in result)
Console.WriteLine(item);
}
static List<int> foo(int total, int minrange4, int maxrange4, int count)
{
Random rnd = new Random();
int r4 = rnd.Next(minrange4 > total ? total - count : minrange4,
maxrange4 < total ? maxrange4 : total - count);
var list = new List<int>();
while (list.Count() != count - 1)
{
int r = rnd.Next(0, total - r4 - list.Sum() - count + list.Count());
list.Add(r);
}
list.Add(total - list.Sum() - r4);
list.Add(r4);
return list;
}
}
}
527
115
241
2117
Press any key to continue . . .
270
21
67
2642
Press any key to continue . . .