#! /bin/bash
#
# uniformity - an emperical test for randomness (in particular, a test for
#   uniform distribution of random numbers, which is not a sufficient condition
#   for randomness).  Reads one integer in the range [0, 99] per line from
#   std-in and writes to std-out the related chi-square statistic with 9
#   degrees of freedom.  A value between [4.17, 14.7] indicates an at-least 1
#   in 10 chance of being wrong when declaring the input numbers non-random
#   (8.34 is the 1 in 2 value). Example use:
#
#   i=1
#   while (( i <= 100 )) ; do 
#     echo $(expr $RANDOM % 100)
#     (( i++ ))
#   done | uniformity

gawk 'BEGIN {
        for (i = 0; i < 10; i++)
	  buckets[i] = 0
        }

      { buckets[int($0/10)]++ }

      END {
        sum = 0
        for (i = 0; i < 10; i++)
	  sum += (buckets[i]*buckets[i])/0.1
	sum = sum/NR - NR
	printf "%4.2f [4.17, 8.34, 14.7]", sum
        }
     '

# $Log: uniformity,v $
# Revision 1.2  2005/04/04 03:09:15  rclayton
# Added the chi-square values.
#


syntax highlighted by Code2HTML, v. 0.9