解决给定的值随机偏离导航

2023-09-11 00:19:22 作者:已定成败。

我想是一种波动的增加的已知类型的值双的数组。我指出这一点,因为Random.Next / Random.NextDouble()的行为不同。

I would like to add a kind of ripple to an array of known values of type double. I point that out because Random.Next / Random.NextDouble() behave different.

我如何最好地实现这个任务?

How do i best achieve this task?

让说,我有数组中的20个值, 名单,其中,双> ARR =新的名单,其中,双>(){40,40,40,40 ......}; 20值其被平均上量40,总的800,以使其更易于

Let say i have 20 values in the array, List<double> arr = new List<double>() { 40, 40, 40, 40 ..... }; 20 values which are averaged on amount 40, with a total of 800, to make it easier.

这个方法之后,我想的整体总的仍然是800,但每个单独的值应该奔修改。该值应该是积极的,因为他们是共有+ = I 后来添加。

After this method, I would like the overall total still remain 800, but each individual value should ben modified. The values should be positive, because they are total+=i added afterwards.

到目前为止此问题是使用的值的给定数量的百分比解决。 1.0 / 20 = 0.05,那么多样信息,与总的迭代次数。然后减去从其余的结果。最后,我只是用新的GUID返回排序()

So far this problem are solved using a percentage of the given quantity of values. 1.0 / 20 = 0.05, then multiplicate that with the total and the iteration number. Then subtract the result from the remainder. Finally i just return a sort by new Guid().

正如你已经看到,这种方法是只有一点点了不起的,虽然只是约5-20值。在今天我的情况下,这个数组需要站在500-2000值(0,2-0,05%每个值)。

As you already see, this method was only a little terrific, while there was just about 5-20 values. In my case today, this array need to stand 500-2000 values (0,2-0,05% each value).

相反,我想有一个衍生物或如此,使以价值40作为基地+ -x%失真。或者,在任何数组中的单个值)。甚至更好,+ -x%

Instead I would like to have a derivative or such, that make a distortion with +-x% of value 40 as base. Or, perhaps even better, +-x% on whatever single value in the array).

[更新]

我会添加基于对这个问题的答复更新的问题。

I will add the question with an update based on the replies on this question.

    Random rnd = new Random();
    List<double> ripple = new List<double>();

    int qty = bArray.Count();
    double diff = last.Value - first.Value;

    if (qty == 1)
    {
        double linearAvg = (diff / qty) / 2;
        ripple.Add(linearAvg);
    }
    else
    {
        double[] rndarr = new double[qty];

        for (int i = 0; i < qty; i++)
            rndarr[i] = rnd.NextDouble();

        double rndArrSum = rndarr.Sum();

        for (int i = 0; i < qty; i++)
            rndarr[i] /= rndArrSum; 

        for (int i = 0; i < qty; i++)
            ripple.Add(diff * rndarr[i]);
    }

    double valueOverall = first.Value;
    for (int i = (qty > 1) ? 1 : 0; i < qty; i++)
        valueOverall += ripple[i];

津贴已为最后生成的值不重叠。另外,异常时能列表仅包含两个值。该数量= 1 可能看起来神奇,但它是指对象如何bArray看上去像真的。我觉得整个思路是清晰的反正。

Allowance has been made for the last generated value not to overlap. In addition, an exception when the list contains only two values​​. The qty=1 may look magic, but it refers to how the object bArray looks like in real. I think the whole idea is clear anyway.

推荐答案

您可以做到这一点的方法之一是产生0和1之间(不含)N个随机数。总结他们。然后将每个数由总和。你现在有N个随机数的列表,总和为1。现在,你需要的总和乘以每个这些数字的获得,将进入你的最后一个数组中的数字。

One way you could do this is to generate N random numbers between 0 and 1 (exclusive). Sum them. Then divide each number by the sum. You now have a list of N random numbers that sum to 1. Now, multiply each of those numbers by your desired sum to get the numbers that will go into your final array.

如果你想你的价值观是+/-某个百分比,然后用 Random.Next 生成某些范围内的随机数,总结他们。然后由总除以得到的数字求和为1的最后一步是相同的列表。

If you want your values to be +/- some percentage, then use Random.Next to generate the random numbers within some range and sum them. Then divide by the total to get the list of numbers that sums to 1. The final step is the same.

相关推荐