#!/usr/bin/perl
#
# Generate random numbers of customers per hour who
#    try to pay for their food with a $20 bill.
#
# MWR 4/12/2010

use POSIX;
use Math::Random;

$debug = 0;

# use the first line for new and different random values each time
#     this script is invoked, or the second for the same old numbers each time
$seed = $$;
#$seed = 1;
srand($seed);

# what should the mean value of customers per hour be?
$mean_value = 10;

# how many hours per day to do we watch?
$num_hours = 10;
# how many stores are we monitoring?
$num_stores = 100;
# how many days do we include in our study?
$num_days = 100;

# set all elements of the histogram to zero
$max_hist = 1000000;
for ($i = 0; $i < $max_hist; $i++) {
  $hist_array[$i] = 0;
}

for ($i_day = 0; $i_day < $num_days; $i_day++) {


  for ($i_hour = 0; $i_hour < $num_hours; $i_hour++) {
    $count = 0;
    for ($i_stores = 0; $i_stores < $num_stores; $i_stores++) {
      $customers = Math::Random::random_poisson(1, $mean_value);
      if ($debug > 0) {
        printf " day %5d store %6d hour %2d  cus %4d \n", 
               $i_day, $i_stores, $i_hour, $customers;
      }
      $count += $customers;
    }
    $hist_array[$count]++;

    if ($debug > 0) {
      printf " Totstores day %5d store %6d count %8d \n", 
               $i_day, $i_stores, $count;
    }
  }

  if ($debug > 0) {
    printf " Totday day %5d  count %8d \n", $i_day, $count;
  }

}


# print out the final histogram
#   first, figure out the last bin we need to print
$last_bin = 0;
for ($i = 0; $i < $max_hist; $i++) {
  if ($hist_array[$i] > 0) {
    $last_bin = $i;
  }
}
# now print the histogram, from 0 to the last bin + 1
for ($i = 0; $i < $last_bin; $i++) {
  printf " %5d  %8d \n", $i, $hist_array[$i];
}
  





exit 0;
