Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to weight your results. You can do that with something like this:</p> <pre><code>private int[] _distribution = new int[] { 0, 1, 2, 3, 4, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9 }; Random _r = new Random(); public int GetWeightedRandom() { return _distribution[_r.Next(0, _distribution.Length)]; } </code></pre> <p>If I knew my range was small and consistent, I'd use the table - it's trivial to make it its own class.</p> <p>For completeness, I'll also add this class in. This class borrows from image processing and uses the gamma correction function: a value between 0 and 1 raised to gamma, which returns a value between 0 and 1 but distributed more to the low end if gamma &lt; 1.0 and more to the high end if gamma > 1.0.</p> <pre><code>public class GammaRandom { double _gamma; Random _r; public GammaRandom(double gamma) { if (gamma &lt;= 0) throw new ArgumentOutOfRangeException("gamma"); _gamma = gamma; _r = new Random(); } public int Next(int low, int high) { if (high &lt;= low) throw new ArgumentOutOfRangeException("high"); double rand = _r.NextDouble(); rand = math.Pow(rand, _gamma); return (int)((high - low) * rand) + low; } } </code></pre> <p>(from comments, moved r out of GetWeightedRandom(). Also added range checking to Next())</p> <p>OK, let's really go to town here. I'm channeling John skeet for this - it's an abstract class with a template property that returns a transform function that maps the range [0..1) to [0..1) and scales the random number to that range. I also reimplemented gamma in terms of it and implemented sin and cos as well.</p> <pre><code>public abstract class DelegatedRandom { private Random _r = new Random(); public int Next(int low, int high) { if (high &gt;= low) throw new ArgumentOutOfRangeException("high"); double rand = _r.NextDouble(); rand = Transform(rand); if (rand &gt;= 1.0 || rand &lt; 0) throw new Exception("internal error - expected transform to be between 0 and 1"); return (int)((high - low) * rand) + low; } protected abstract Func&lt;double, double&gt; Transform { get; } } public class SinRandom : DelegatedRandom { private static double pihalf = Math.PI / 2; protected override Func&lt;double, double&gt; Transform { get { return r =&gt; Math.Sin(r * pihalf); } } } public class CosRandom : DelegatedRandom { private static double pihalf = Math.PI / 2; protected override Func&lt;double, double&gt; Transform { get { return r =&gt; Math.Cos(r * pihalf); } } } public class GammaRandom : DelegatedRandom { private double _gamma; public GammaRandom(double gamma) { if (gamma &lt;= 0) throw new ArgumentOutOfRangeException("gamma"); _gamma = gamma; } protected override Func&lt;double, double&gt; Transform { get { return r =&gt; Math.Pow(r, _gamma); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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