#!/usr/bin/perl -w
#-- FILE:   range_generator.pl
#-- CODER:  KUBO Takuya (kubo@ees.hokudai.ac.jp)
#-- 
#-- (C)opyright 2001 KUBO Takuya (kubo@ees.hokudai.ac.jp).
#--    All rights are reserved.
#--    Do not modify without amending copyright,
#--    distribute freely but retain this paragraph.
#-- 
#-- CHANGE: Thu Jun 14 09:48:22 2001
#   MODIFY: Thu Jun 14 09:47:38 2001
use strict;

my %value = (
	"sample size"      => 10,
	"sigma min"        => 0.2,
	"sigma max"        => 1.0,
        "sigma change"     => 0.2,
	"number of range"  => 100,
);
printf "# sample size: %i\n", $value{ "sample size" };
printf "# sample size for mean range: %i\n", $value{ "number of range" };

# Generating a Gaussian random number N(0,1)
sub gaussian_random_number {
	my $pi_2 = 3.141592 * 2.0;
	return sqrt( -2.0 * log( rand( 1.0 ) ) ) * cos( $pi_2 * rand( 1.0 ) );
} # Box-Muller method

# Generating a range of PD
sub calc_range {
	my ( $sample_size, $sigma ) = @_;
	my @random_number = ();
	for ( my $i = 0; $i < $sample_size; $i++ ) {
		push @random_number, $sigma * gaussian_random_number;
	}
	my @sorted_random_number = sort { $a <=> $b } @random_number;
	my $range = $sorted_random_number[ -1 ] - $sorted_random_number[ 0 ];
	return $range; # $range = MAX - MIN
}

for (
	my $sigma = $value{ "sigma min" };
	$sigma <= $value{ "sigma max" };
	$sigma += $value{ "sigma change" }
) {
	my $mean_range = 0;
	for ( my $i = 0; $i < $value{ "number of range" }; $i++ ) {
		$mean_range += calc_range( $value{ "sample size" }, $sigma );
	}
	$mean_range /= $value{ "number of range" };
	printf "sigma: %.2f\tmean_range: %.2f\n", $sigma, $mean_range;
}

exit 0;
