Note that there are some explanatory texts on larger screens.

plurals
  1. POGet a randomized aberrancy around given value
    text
    copied!<p>I would like to add a kind of ripple to an array of known values of type <strong>double</strong>. I point that out because Random.Next / Random.NextDouble() behave different.</p> <p>How do i best achieve this task?</p> <p>Let say i have 20 values in the array, <br> <code>List&lt;double&gt; arr = new List&lt;double&gt;() { 40, 40, 40, 40 ..... };</code><br> 20 values which are averaged on amount 40, with a total of 800, to make it easier.</p> <p>After this method, I would like the <em>overall total</em> still remain 800, but each individual value should ben modified. The values should be positive, because they are <code>total+=i</code> added afterwards.</p> <p>So far this problem are solved using a percentage of the given quantity of values. <br> <code>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()</code>.</p> <p>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). </p> <p>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).</p> <p><strong>[UPDATE]</strong></p> <p>I will add the question with an update based on the replies on this question.</p> <pre><code> Random rnd = new Random(); List&lt;double&gt; ripple = new List&lt;double&gt;(); 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 &lt; qty; i++) rndarr[i] = rnd.NextDouble(); double rndArrSum = rndarr.Sum(); for (int i = 0; i &lt; qty; i++) rndarr[i] /= rndArrSum; for (int i = 0; i &lt; qty; i++) ripple.Add(diff * rndarr[i]); } double valueOverall = first.Value; for (int i = (qty &gt; 1) ? 1 : 0; i &lt; qty; i++) valueOverall += ripple[i]; </code></pre> <p>Allowance has been made for the last generated value not to overlap. In addition, an exception when the list contains only two values​​. The <code>qty=1</code> may look magic, but it refers to how the object bArray looks like in real. I think the whole idea is clear anyway.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload