Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数字列表中随机选择,直到达到特定限制_C# - Fatal编程技术网

C# 从数字列表中随机选择,直到达到特定限制

C# 从数字列表中随机选择,直到达到特定限制,c#,C#,假设我有一个预定义的数字列表和一个预定义的最大限制列表 当用户选择限制时,我需要从第一个列表中随机选择一定数量的数字,直到它们的总数与用户选择的总数相匹配(尽可能接近,但永远不会超过) 到目前为止,我所尝试的: void Main() { List<int> num = new List<int>(){ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20 }; int m

假设我有一个预定义的数字列表和一个预定义的最大限制列表

当用户选择限制时,我需要从第一个列表中随机选择一定数量的数字,直到它们的总数与用户选择的总数相匹配(尽可能接近,但永远不会超过)

到目前为止,我所尝试的:

void Main()
{
    List<int> num = new List<int>(){ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20 };
    int maxNum = 17;

    List<int> curNum = new List<int>();
    int curTotal = 0;

    foreach(int sel in num.Where(x => x < maxNum)){

    curTotal += sel;

    if(curTotal <= maxNum){
        curNum.Add(sel);
    }


    }
}
void Main()
{
List num=new List(){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int maxNum=17;
List curNum=新列表();
int-curTotal=0;
foreach(整数选择在num.Where中(x=>x
大致如下:

var shuffledList = num.ToList();
shuffledList.Shuffle();

var sum = 0;
var count = 0;
while (shuffledList[count] + sum < max)
{ 
    sum += shuffledList[count++];
}
return shuffledList.Take(count);
var shuffledList=num.ToList();
shuffledList.Shuffle();
var总和=0;
var计数=0;
while(无序列表[计数]+总和<最大值)
{ 
sum+=shuffledList[count++];
}
返回shuffledList.Take(计数);

基于@AlexiLevenkov的答案:

班级计划 { 静态void Main(字符串[]参数) { 整数极限=17; int listSize=5

        List<int> a = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        a.Shuffle();
        List<int> genList = new List<int>();
        int stoppedCount = 0;

        for (int i = 0; i < a.Count(); i++)
        {

            if (i < listSize)
            {
                genList.Add(a[i]);
                stoppedCount = i;
            }
            else
            {
                break;
            }
        }

        while (genList.Sum() > limit)
        {
            genList.Remove(genList.Max());

            stoppedCount++;
            genList.Add(a[stoppedCount]);
        }
    }

}

static class ThisClass
{
    public static void Shuffle<T>(this IList<T> list)
    {
        Random rng = new Random();
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}
List a=newlist(){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
a、 洗牌();
List genList=新列表();
int stoppedCount=0;
对于(int i=0;i限制)
{
删除(genList.Max());
stoppedCount++;
添加(一个[stoppedCount]);
}
}
}
静态类ThisClass
{
公共静态无效洗牌(此IList列表)
{
随机rng=新随机();
int n=list.Count;
而(n>1)
{
n--;
int k=下一个(n+1);
T值=列表[k];
列表[k]=列表[n];
列表[n]=值;
}
}
}

这称为随机交易。最好从最大的数字开始,再加上逐渐变小的部分。我忘了提到一个关键因素!:/n这是怎么回事:如果最后一个随机数太大(超过总数),就用剩余的(总和)作为最后一个“随机数”?数字不能动态创建。它必须来自列表。我得到的范围为0-5项,其中包含this@SemiDemented我认为“+=”应该解决这个问题(请参阅更新的代码,根本没有测试)