Home   Cover Cover Cover Cover
 

Normal distribution

/csbook/solutions/18/A04.cs
using System;

class NormalDistribution {
  
  static Random rand = new Random();
  
  // returns a normally distributed random number in the range [0..1[
  static double NextNormal() {
    double x = 0;
    for (int i = 0; i < 5; i++) x += rand.NextDouble();
    return x / 5;
  }
  
  static void Main(string[] arg) {
    int[] tab = new int[20];
    // fill tab
    for (int i = 0; i < 200; i++) tab[(int)(20 * NextNormal())]++;
    // print tab as a histogram
    for (int i = 0; i < 20; i++) {
      Console.Write("{0,2}: ", i);
      for (int j = 0; j < tab[i]; j++) Console.Write("*");
      Console.WriteLine();
    }
  }
}