Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understood well, you just need a function that return a random numbered "centered" on bias and always inside the {min;max} range...</p> <p>what about:</p> <pre><code>public double getBiasedRandom(double bias, double min, double max) { double bias_depth_perc = 0.1; double bias_depth_abs = (max - min)*bias_depth_perc; double min_bias = bias - bias_depth; double max_bias = bias + bias_depth; Random tRandom = new Random(); if (max_bias &gt; max) max_bias = max; if (min_bias &lt; min) min_bias = min; double variance = (max_bias - min_bias)/2; double rndBiased = bias + tRandom .nextGaussian() * aVariance; if (rndBiased &gt; max) rndBiased = max - (rndBiased - max); if (rndBiased &lt; min) rndBiased = min + (min - rndBiased); return rndBiased; } </code></pre> <p>practically you get the biased random number simply using a gaussian distribution centered on bias with a variance that you can set to a percentage of range (set in the example to 10% on the row double bias_depth_perc = 0.1).</p> <p><strong>EDIT</strong>: changed the behaviour when the bias is close to the boundary and you get a lot of random number outside the limits. Now it simply copy the random generated number inside the boundary with a distance equal to the distance from the limit itself. This ends actually in a simple asymmetrical distribution.</p> <p>SECOND SOLUTION: this is a bit more tricky. Here the idea is to always generates a symmetrical random number, and then map the generated number on the right range. For example: if you have:</p> <ul> <li>min = 0</li> <li>max = 1</li> <li>bias = 0.1</li> </ul> <p>you firstly generate a random number centered on 0.5 with a variance of your choice:</p> <ul> <li>rnd = 0.5 tRandom.nextGaussian() * variance;</li> </ul> <p>then, if rnd is > 0.5, you map it into the range {bias; bias + max_bias} with a simple scale moltiplication. if rnd is &lt; 0.5, you map it into the range {min_bias; bias}.</p> <p>here is the code:</p> <pre><code>public double getBiasedRandom(double bias, double min, double max) { double centered_depth_perc = 0.3; double centered_depth_abs = (max - min)*centered_depth_perc; double center = 0.5; Random tRandom = new Random(); double rndCentered = center + tRandom .nextGaussian() * centered_depth_abs; // generate centered random number. if (rndCentered &gt;= center) rndBiased = (rndCentered - center) * (max - bias) + bias; else rndBiased = bias - (center - rndCentered) * (bias - min); // the following two tests will be as more important as centered_depth_perc // get bigger. if (rndBiased &gt; max) rndBiased = max; if (rndBiased &lt; min) rndBiased = min; return rndBiased; } </code></pre> <p>hope it could help.</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