File: decay.pl

package info (click to toggle)
libmath-random-mt-auto-perl 6.23-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 576 kB
  • sloc: perl: 1,068; makefile: 3
file content (53 lines) | stat: -rwxr-xr-x 1,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/perl

# Produces a decay curve using exponential random numbers.
# Uses a mean of 15, and computes 80 'bins' of data.

# Usage:  decay.pl [COUNT]
#         COUNT defaults to 1 million

use strict;
use warnings;

$| = 1;

use Math::Random::MT::Auto qw(exponential);

MAIN:
{
    my $count = (@ARGV) ? $ARGV[0] : 1000000;

    my %decay;

    # Get random numbers and put them in bins
    print("Generating $count exponential random numbers.  Please wait...");
    for (1 .. $count) {
        my $x = int(exponential(15));

        # Make sure the tail doesn't overflow
        if ($x > 80) {
            $x = 80;
        }

        $decay{$x}++;
    }

    # Find the max bin size for scaling the output
    my $max = 0;
    while (my $key = each(%decay)) {
        if ($max < $decay{$key}) {
            $max = $decay{$key};
        }
    }

    # Output the graph
    print("\n");
    for my $key (sort { $a <=> $b } (keys(%decay))) {
        my $len = int(79.0 * $decay{$key}/$max);
        print(':', '*' x $len, "\n");
    }
}

exit(0);

# EOF