Content-Type: TEXT/PLAIN; charset=US-ASCII; name=dice
Content-ID: <Pine.SGI.3.95.961205113330.6349P@suntsu.uni-c.dk>
Content-Description: dice shell script

#!/bin/sh
#
# Die roll generator for use by PBeM gamers.
# Generates a seed that is fed to the program diceroll,
# which in turn generates the specified number of die rolls.
# The script expects a mail with the body containing lines:
# "#P" <mail address(es) of players> (Multiple lines allowed)
# "#S" <no. of sides on every die>
# "#D" <no. of dice for every roll>
# "#R" <no. of rolls requested>
# "#L" <no. of rolls printed pr. line in output>
# "#C" <any comment, forwarded with result> (Multiple lines allowed)
# "#T" <subject line of returned mail>
#
PATH=/usr/local/pp/cmds:/usr/local/pp/bin:/usr/local/pp/cmds/tools:/usr/lib/bin:/usr/ucb:/bin:/usr/bin:/usr/5bin:/home/uniel/bin:
export PATH
egrep '(#P|#S|#D|#R|#C|#L|#T)' > /tmp/dice.$$
grep "#P" /tmp/dice.$$ | sed 's/#P//' | sed 's/dice@danpost4.uni-c.dk//' | sed 's/dice//'  > /tmp/dice.$$.1
echo "Dice rolls requested by: " $* > /tmp/dice.$$.3
echo "" >> /tmp/dice.$$.3
grep "#C" /tmp/dice.$$ | sed 's/#C /# /' | sed 's/#C/# /' >> /tmp/dice.$$.3
echo "" >> /tmp/dice.$$.3
#date +%j%M%S%y%j > /tmp/dice.$$.2
#date +%M%S%y%j > /tmp/dice.$$.2
date +%M%S > /tmp/dice.$$.2
grep "#S" /tmp/dice.$$ | sed 's/#S //'  >> /tmp/dice.$$.2
grep "#D" /tmp/dice.$$ | sed 's/#D //'  >> /tmp/dice.$$.2
grep "#R" /tmp/dice.$$ | sed 's/#R //'  >> /tmp/dice.$$.2
if grep "#L" /tmp/dice.$$  > /dev/null
then
	grep "#L" /tmp/dice.$$ | sed 's/#L //'  >>/tmp/dice.$$.2
else
	echo "10" >> /tmp/dice.$$.2
fi
if grep "#T" /tmp/dice.$$  > /dev/null
then
	grep "#T" /tmp/dice.$$ | sed 's/#T //'  > /tmp/dice.$$.4
else
	echo "Dice rolls" > /tmp/dice.$$.4
fi
if diceroll < /tmp/dice.$$.2 > /dev/null
then
	diceroll < /tmp/dice.$$.2 >> /tmp/dice.$$.3
else
	diceroll < /tmp/dice.$$.2 >> /tmp/dice.$$.3
	echo "" >> /tmp/dice.$$.3
	echo "Usage of dice roller:" >> /tmp/dice.$$.3
	echo "" >> /tmp/dice.$$.3
	echo "Send a mail to dice@danpost4.uni-c.dk containing:" >> /tmp/dice.$$.3
	echo "" >> /tmp/dice.$$.3
	echo "#P <mail address(es) of players> (Multiple lines allowed)" >> /tmp/dice.$$.3
	echo "#S <no. of sides on every die>" >> /tmp/dice.$$.3
	echo "#D <no. of dice for every roll>" >> /tmp/dice.$$.3
	echo "#R <no. of rolls requested>" >> /tmp/dice.$$.3
	echo "#L <no. of rolls printed pr. line in output>" >> /tmp/dice.$$.3
	echo "#C <any comment(s), forwarded with result> (Multiple lines allowed)" >> /tmp/dice.$$.3
	echo "#T <subject line of returned mail>" >> /tmp/dice.$$.3
fi
echo "" >> /tmp/dice.$$.3
echo "*******************************************************" >> /tmp/dice.$$.3
echo "* IMPORTANT NOTE:                                     *" >> /tmp/dice.$$.3
echo "*                                                     *" >> /tmp/dice.$$.3
echo "*   DON'T REPLY to this message from the dice roller. *" >> /tmp/dice.$$.3
echo "*   Replies end up in the administrators mailbox.     *" >> /tmp/dice.$$.3
echo "*                                                     *" >> /tmp/dice.$$.3
echo "*******************************************************" >> /tmp/dice.$$.3
mail.pp -t $* `cat /tmp/dice.$$.1` -s "`cat /tmp/dice.$$.4`" < /tmp/dice.$$.3
# Log 
L=/home/uniel/log/dice.log
if [ -f $L ]
then
	echo "`date`: Dice requested by: $*" >> $L
	echo "input:" >> $L
	cat /tmp/dice.$$.2 >> $L
else
	echo "`date`: Dice requested by: $*" > $L
	echo "input:" >> $L
	cat /tmp/dice.$$.2 >> $L
fi
rm /tmp/dice.

$$*Content-Type: TEXT/PLAIN; charset=US-ASCII; name="diceroll.c"
Content-ID: <Pine.SGI.3.95.961205113330.6349Q@suntsu.uni-c.dk>
Content-Description: dice roll program

/* 
  Generates dice rolls.
  5 input parameters are required:
   seed -  Seed to the random function.
   sides - The number of sides on every die.
   dice  - The number of dice.
   rolls - The number of dice rolls requested.
   prlin - The number of dice rolls printed pr. line in output.
*/
#include <stdio.h>
unsigned long seed;
unsigned int sides, dice, test, prlin;

main()
  {
    unsigned long rolls, i;
    unsigned int die();

    test = 0;
    scanf("%lu", &seed);
    scanf("%iu", &sides);
    scanf("%iu", &dice);
    scanf("%lu", &rolls);
    scanf("%lu", &prlin);
    printf("\n");
    if ( ( 0 < sides && sides < 1001) &&
         ( 0 < dice  && dice  < 1001) &&
         ( 0 < rolls && rolls < 1001) &&
         ( 0 < prlin && prlin < 21) ) 
      {
        printf("Random seed:                 %4u\n",seed);
        printf("No. of sides on every die:   %4u\n",sides);
        printf("No. of dice for every roll:  %4u\n",dice);
        printf("No. of dice rolls requested: %4u\n",rolls);
        printf("No. of rolls pr. line:       %4u\n\n",prlin);
        for( i=1 ; i <= rolls ; ++i ) 
           printf( (i%prlin) ? "%4u  " : "%4u\n", die() );
        printf("\n");
        printf("\n");
        printf("Average of dice rolls: %5.2f\n", (double)test/(double)rolls);
        printf("\n");
        exit(0);
      }
    else
      {
        printf("No. of sides, dice, and rolls must be in [1..1000]\n");
        printf("and no. of rolls output pr. line must be in [1..20]\n");
        printf("Input to dice roll program was: %6u %6u %6u %6u\n",sides,dice,rolls,prlin);
        exit(1);
      }
  }

unsigned int die()
  {
    double random();
    unsigned int i, sum;
    /* One should be sure the global variables seed and dice are initialized
       before the call to random() */
    sum = 0;
    for( i=1 ; i <= dice ; ++i ) 
       sum = sum + (random()*sides) + 1;
    test = test + sum;
    return (unsigned int) sum;
   }

double random()
/* Before calling this for the first time, the global variable seed
   needs to be initialized.
*/
   {
    static unsigned long a = 25173L, c = 13849L, m = 65536L;
    seed = (a*seed+c) % m;
    return (double) seed / (double) (m-1);
   }