C#随机算法直到满足条件 [英] C# Random algorithm until fulfills condition
本文介绍了C#随机算法直到满足条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下情况:
- 生成
n
个随机数一定范围内 - 所有数字求和
- 检查
sum == x
(x是用户设置的数字) - 如果
sum!= x
然后继续运行循环 - 如果
sum == x
,则显示总计为x
的随机数列表li>
- Generate
n
numbers of random number from a certain range - Sum all the numbers
- Check if the
sum == x
(x is a number set by user) - if
sum != x
then keep running the loop - If
sum == x
, then display the list of random numbers that sum up tox
基于此逻辑,我能够做到这一点,但是要永远达到结果,是否有更好的方法来解决这个问题
Based on this logic, I was able to do so but it takes forever to achieve the result, is there any better / way to solve this problem?
static void Main(string[] args)
{
Console.WriteLine("starting...");
List<int> checkList = generate(5);
while(!checkSum(checkList, 100))
{
checkList = generate(5)
}
Console.WriteLine("done!");
}
private static bool checkSum(List<int> n, int sum)
{
if(n.Sum() == sum)
{
return true;
}
else
{
return false;
}
}
public static List<int> generate(int n)
{
Random rng = new Random();
List<int> list = new List<int>();
for (int i = 0; i < 5; i++)
{
//int ran = some random number
list.Add(ran);
}
return list;
}
编辑
在这里,我的情况是获取总计为100的随机整数的 n
个组合。组合的数量取自用户的输入。因此,程序将给出 n
个可能的组合的总数,总计为100。
My scenario here is to obtain n
combinations of random integer that sums up to 100. The number of combinations is taken from input by user. So the program will give the n
number of possible combinations that sums up to 100.
可能的组合:
- 25 + 37 + 9 + 20 + 9 = 100
- 46 + 21 + 13 + 8 + 12 = 100
推荐答案
如果您有固定的总和和固定数量的元素, ,将问题视为分区。例如:
If you have a fixed sum and a fixed number of elements you want, look at the problem as partitioning. For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PartitionAndAllocateTEst
{
class Program
{
static void Main(string[] args)
{
var rand = new Random();
int desiredTotal = 100;
int partitions = 5;
for (int i = 0; i < 10; i++)
{
List<int> result = GetRandomCollection(rand, desiredTotal, partitions);
Console.WriteLine(string.Join(", ", result.Select(r => r.ToString()).ToArray()));
}
}
private static List<int> GetRandomCollection(Random rand, int desiredTotal, int partitions)
{
// calculate the weights
var weights = new List<double>();
for (int i = 0; i < partitions; i++)
{
weights.Add(rand.NextDouble());
}
var totalWeight = weights.Sum();
// allocate the integer total by weight
// http://softwareengineering.stackexchange.com/questions/340393/allocating-an-integer-sum-proportionally-to-a-set-of-reals/340394#340394
var result = new List<int>();
double allocatedWeight = 0;
int allocatedCount = 0;
foreach (var weight in weights)
{
var newAllocatedWeight = allocatedWeight + weight;
var newAllocatedCount = (int)(desiredTotal * (newAllocatedWeight / totalWeight));
var thisAllocatedCount = newAllocatedCount - allocatedCount;
allocatedCount = newAllocatedCount;
allocatedWeight = newAllocatedWeight;
result.Add(thisAllocatedCount);
}
return result;
}
}
}
示例输出:
30, 6, 19, 15, 30
36, 8, 22, 10, 24
2, 25, 32, 21, 20
22, 7, 30, 12, 29
36, 21, 22, 0, 21
24, 24, 2, 29, 21
18, 13, 10, 39, 20
11, 19, 20, 27, 23
24, 19, 7, 25, 25
24, 14, 27, 18, 17
这篇关于C#随机算法直到满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文