title: triggering an event with a certain probability with C#
body: <p>I'm trying to simulate a realistic key press event. For that reason I'm using SendInput() method, but for greater result I need to specify the delay between keyDOWN and KeyUP events! These numbers below show the elapsed time in milliseconds between DOWN and UP events (these are real/valid):</p>
  
  <p>96
  95
  112
  111
  119
  104
  143
  96
  95
  104
  120
  112
  111
  88
  104
  119
  111
  103
  95
  104
  95
  127
  112
  143
  144
  142
  143
  128
  144
  112
  111
  112
  120
  128
  111
  135
  118
  147
  96
  135
  103
  64
  64
  87
  79
  112
  88
  111
  111
  112
  111
  104
  87
  95</p>
  
  <p><strong>We can simplify the output:</strong></p>
  
  <p>delay 64 - 88 ms -> 20% of a time</p>
  
  <p>delay 89 - 135 ms -> 60% of a time</p>
  
  <p>delay 136 - 150 ms -> 20 % of a time</p>
  
  <p><strong>How do I trigger an event according to probabilities from above?</strong> Here is the code I'm using right now:</p>
  
  <pre><code>        private void button2_Click(object sender, EventArgs e)
          {
              textBox2.Focus();
              Random r = new Random();
              int rez = r.Next(0, 5); // 0,1,2,3,4 - five numbers total
  
              if (rez == 0) // if 20% (1/5)
              {
                  Random r2 = new Random();
                  textBox2.AppendText(" " + rez + " " + r2.Next(64, 88) + Environment.NewLine);
  // do stuff
              }
              else if (rez == 4)//if 20% (1/5)
              {
                  Random r3 = new Random();
                  textBox2.AppendText(" " + rez + " " + r3.Next(89, 135) + Environment.NewLine);
  // do stuff
  
              }
              else // if 1 or 2 or 3 (3/5) -&gt; 60%
              {
                  Random r4 = new Random();
                  textBox2.AppendText(" " + rez + " " + r4.Next(136, 150) + Environment.NewLine);
  // do stuff
  
              }
  
          }
  </code></pre>
  
  <p>There is a huge problem with this code. In theory, after millions of iterations - the resulting graph will look similar to this:</p>
  
  <p><img src="https://i.stack.imgur.com/nIB5A.png" alt="comparison"></p>
  
  <p>How to deal with this problem?</p>
  
  <p><strong>EDIT:</strong> the solution was to use distribution as people suggested.</p>
  
  <p>here is java implementation of such code:</p>
  
  <p><a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#nextGaussian%28%29" rel="nofollow noreferrer">http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#nextGaussian%28%29</a></p>
  
  <p>and here is C# implementation:</p>
  
  <p><a href="https://stackoverflow.com/questions/1303368/how-to-generate-normally-distributed-random-from-an-integer-range">How to generate normally distributed random from an integer range?</a></p>
  
  <p>although I'd suggest to decrease the value of "deviations" a little.</p>
  
  <p>here is interesting msdn article</p>
  
  <p><a href="http://blogs.msdn.com/b/ericlippert/archive/2012/02/21/generating-random-non-uniform-data-in-c.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/ericlippert/archive/2012/02/21/generating-random-non-uniform-data-in-c.aspx</a></p>
  
  <p>everyone thanks for help!</p>