Blame test/randtest.sh

Packit 575503
# THIS PURPOSELY DOES NOT HAVE A !# LINE !!!!
Packit 575503
#
Packit 575503
# Date: Mon, 9 Sep 2013 14:49:43 -0700
Packit 575503
# From: Bob Jewett <jewett@bill.scs.agilent.com>
Packit 575503
# Message-Id: <201309092149.r89Lnh94010909@bill.scs.agilent.com>
Packit 575503
# To: arnold@skeeve.com
Packit 575503
# Subject: Re: [bug-gawk] Bug in random() in builtin.c
Packit 575503
# 
Packit 575503
# Hi Arnold,
Packit 575503
# 
Packit 575503
# Attached below is a script that tests gawk for this particular
Packit 575503
# rand() problem.  The pair-wise combinations show a strong
Packit 575503
# autocorrelation for a delay of 31 pairs of rand() samples. 
Packit 575503
# 
Packit 575503
# The script prints out the measured autocorrelation for a record
Packit 575503
# of NSAMPLES pairs.  It also prints a fail message at the end if
Packit 575503
# it fails. 
Packit 575503
# 
Packit 575503
# If you want to see the autocorrelation values, there is a print
Packit 575503
# statement that if uncommented will save them to a file.
Packit 575503
# 
Packit 575503
# Please let me know if the mailer screws up the transfer or
Packit 575503
# if you have any questions about the test.
Packit 575503
# 
Packit 575503
# Best regards,
Packit 575503
# Bob
Packit 575503
# 
Packit 575503
# -------------- test_pair_power_autocorrelation -----------------------
Packit 575503
# 
Packit 575503
#!/bin/ksh
Packit 575503
Packit 575503
#GAWK=/bin/gawk
Packit 575503
Packit 575503
# ADR: Get GAWK from the environment.
Packit 575503
# Additional note: This wants ksh/bash for the use of $RANDOM below to
Packit 575503
# seed the generator. However, shells that don't provide it won't be
Packit 575503
# a problem since gawk will then seed the generator with the time of day,
Packit 575503
# as srand() will be called without an argument.
Packit 575503
Packit 575503
# large NSAMPLES and NRUNS will bring any correlation out of the noise better
Packit 575503
NSAMPLES=1024; MAX_ALLOWED_SIGMA=5; NRUNS=50;
Packit 575503
Packit 575503
$GAWK 'BEGIN{ 
Packit 575503
    srand('$RANDOM');
Packit 575503
    nsamples=('$NSAMPLES');
Packit 575503
    max_allowed_sigma=('$MAX_ALLOWED_SIGMA');
Packit 575503
    nruns=('$NRUNS');
Packit 575503
    for(tau=0;tau
Packit 575503
Packit 575503
    for(run=0;run
Packit 575503
	sum=0;
Packit 575503
Packit 575503
	# Fill an array with a sequence of samples that are a
Packit 575503
	# function of pairs of rand() values.
Packit 575503
Packit 575503
	for(i=0;i
Packit 575503
	   samp[i]=((rand()-0.5)*(rand()-0.5))^2;
Packit 575503
	   sum=sum+samp[i];
Packit 575503
	   }
Packit 575503
Packit 575503
	# Subtract off the mean of the sequence:
Packit 575503
Packit 575503
	mean=sum/nsamples;
Packit 575503
	for(i=0;i
Packit 575503
Packit 575503
	# Calculate an autocorrelation function on the sequence.
Packit 575503
	# Because the values of rand() should be independent, there
Packit 575503
	# should be no peaks in the autocorrelation.
Packit 575503
Packit 575503
	for(tau=0;tau
Packit 575503
	    sum=0;
Packit 575503
	    for(i=0;i
Packit 575503
	    corr[tau]=corr[tau]+sum;
Packit 575503
	    }
Packit 575503
Packit 575503
	}
Packit 575503
    # Normalize the autocorrelation to the tau=0 value.
Packit 575503
Packit 575503
    max_corr=corr[0];
Packit 575503
    for(tau=0;tau
Packit 575503
Packit 575503
    # OPTIONALLY Print out the autocorrelation values:
Packit 575503
Packit 575503
    # for(tau=0;tau<nsamples/2;tau++) print tau, corr[tau] > "pairpower_corr.data";
Packit 575503
Packit 575503
    # Calculate the sigma for the non-zero tau values: 
Packit 575503
Packit 575503
    power_sum=0;
Packit 575503
Packit 575503
    for(tau=1;tau
Packit 575503
Packit 575503
    sigma=sqrt(power_sum/(nsamples/2-1));
Packit 575503
Packit 575503
    # See if any of the correlations exceed a reasonable number of sigma:
Packit 575503
Packit 575503
    passed=1;
Packit 575503
    for(tau=1;tau
Packit 575503
	if ( abs(corr[tau])/sigma > max_allowed_sigma ) {
Packit 575503
	    print "Tau=", tau ", Autocorr=", corr[tau]/sigma, "sigma";
Packit 575503
	    passed=0;
Packit 575503
	    }
Packit 575503
        }
Packit 575503
    if(!passed) {
Packit 575503
	print "Test failed."
Packit 575503
	exit(1);
Packit 575503
        }
Packit 575503
    else exit (0);
Packit 575503
    }
Packit 575503
Packit 575503
function abs(abs_input) { return(sqrt(abs_input^2)) ; }
Packit 575503
'
Packit 575503
Packit 575503
exit 0