/* dice -- simulates dice throws | Copyright (C) 2007 Jannis Schnitzer * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. This program is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * Public License for more details. You should have received a copy of the * GNU General Public License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ #include #include #define false 0 #define true 1 void raw_recursive (int max_i, int layer, int max_layer, void * (*fcn) (void *, int, int), void * arg) { layer++; for (int i = 0; i < max_i; i++) { fcn (arg, layer, i); if (layer < max_layer) { raw_recursive (max_i, layer, max_layer, fcn, arg); } } } int main ( int argc, char *argv[] ) { FILE *rfile = fopen ( "/dev/urandom", "r" ); int seed = getc ( rfile ); srand ( seed ); fclose ( rfile ); int number, count; if ( argc != 3 ) { printf ( "dice needs exactly two arguments!\n" ); printf ( "Usage: dice [Nr. of dices] [Nr. of throws]\n" ); printf ( "Nr. of throws is not bigger than 10000000\n" ); exit ( EXIT_FAILURE ); } number = atoi ( argv[1] ); count = atoi ( argv[2] ); if ( number < 1 || number > 10000000 ) number = 1; if ( count < 1 || count > 10000000 ) count = 100; int result[count]; int nr_of_nr[number * 6]; long int totale = 0; register int i; for ( i = 0; i < ( number * 6 ); i++ ) nr_of_nr[i] = 0; int printout = false; if ( count < 1000 ) printout = true; for ( i = 0; i < count; i++ ) { result[i] = 1 + rand () % ( number * 6 ); nr_of_nr[result[i] - 1]++; totale += ( long int ) result[i]; if ( printout ) { printf ( "%6d ", result[i] ); fflush (stdout); if ( ( i + 1 ) % 10 == 0 ) printf ( "\n" ); } else if ( i % ( count / 100 ) == 0 ) { if ( i ) printf ( "\b\b\b\b\b" ); printf ( "%3d %%", ( int ) ( ( ( ( double ) i / ( double ) count ) * 100.0 ) + 1.0 ) ); fflush (stdout); } } double sum = ( ( double ) totale / ( double ) count ); printf ( "\n" ); printf ( "Average value: %f\n", sum ); for ( i = 0; i < ( number * 6 ); i++ ) printf ( "%3d was thrown %5d (%d) times\n", i + 1, nr_of_nr[i], count / ( number * 6 ) ); exit ( EXIT_SUCCESS ); }