1#!/usr/bin/perl
2
3# Produces a bell curve using Gaussian random numbers.
4# Uses a standard deviation of 10 and a mean of 0.5 so that the '0' bin
5#   contains the randoms from 0 to 1 and so on.
6# Plots +/- 4 standard deviations.
7
8# Usage:  bell.pl [COUNT]
9#         COUNT defaults to 1 million
10
11use strict;
12use warnings;
13
14$| = 1;
15
16use Math::Random::MT::Auto qw(gaussian);
17
18MAIN:
19{
20    my $count = (@ARGV) ? $ARGV[0] : 1000000;
21
22    my %bell;
23
24    # Get random numbers and put them in bins
25    print("Generating $count Gaussian random numbers.  Please wait...");
26    for (1 .. $count) {
27        my $x = gaussian(10, 0.5);
28
29        # Handle 'rounding' using int()
30        if ($x < 0) {
31            $x = int($x-1);
32        } else {
33            $x = int($x);
34        }
35
36        # Make sure the tails don't overflow
37        if ($x > 40) {
38            $x = 40;
39        } elsif ($x < -40) {
40            $x = -40;
41        }
42
43        $bell{$x}++;
44    }
45
46    # Find the max bin size for scaling the output
47    my $max = 0;
48    while (my $key = each(%bell)) {
49        if ($max < $bell{$key}) {
50            $max = $bell{$key};
51        }
52    }
53
54    # Output the graph
55    print("\n");
56    for my $key (sort { $a <=> $b } (keys(%bell))) {
57        my $len = int(79.0 * $bell{$key}/$max);
58        print(':', '*' x $len, "\n");
59    }
60}
61
62exit(0);
63
64# EOF
65