[CoLoCo] dice rolls in C

Kevin Fries kfries at cctus.com
Tue Feb 5 16:40:49 GMT 2008


Also, I forgot about the compilation.

make sure you have installed build-essentials.

Then compile it with gcc.

if you simply use:
$ gcc dicerolls.c

you will get a file named a.out that is your executable.  You can rename
that file by using a -o option.  So:

$ gcc dicerolls.c -o dice

renames a.out to dice.

HTH
Kevin 



On Tue, 2008-02-05 at 09:37 -0700, Kevin Fries wrote:
> Hopefully my comments will help you understand.  Changing from 3 dice to
> two is pretty trivial.  Ask again if this explanation does not help you
> figure it out on your own.
> 
> Kevin
> 
> 
> #include <time.h> /* Needed just for srand seed */
> #include <stdlib.h>
> #include <stdio.h>
> 
> >> These are optional libraries and headers.  Stdlib is included
> >> in most programs, and stdio is included whenever you are outputing
> >> direct from the program (i.e. no x)
> 
> const tenmillion = 1000000L;
>  /* #define tenmillion 10000000L */
> 
> >> This simply defines a constant value to use instead of a number
> 
>  void Randomize() {
>    srand( (unsigned)time( NULL ) ) ;
>  }
> 
> >> This function calls the random number generator with the current time
> >> in milliseconds as a seed to further randomize the value.  The value
> >> returned will be between 0 and 1.
> 
>  int Random(int Max) {
>    return ( rand() % Max)+ 1; 
>  }
> 
> >> This calls the last function and converts it to a number between 0 
> >> and 1 to a number between 1 and Max.
> 
> int main(int argc, char* argv[]) {
>   int i;
> 
> >> This is just a counter variable being declared for future use
> 
>   int totals[19];
> 
> >> This will hold a counter of how many times the three rolls total
> >> a certain value.  Three dice x six sides = 18 values, not sure why
> >> they made it 19.  Think about it, 0, 1, and 2 are also impossible 
> >> values, so this could have been 15 since their are only 15 possible
> >> values, but he made it 18 (19?) to make logic easier later.
> 
>   printf("Rolling Ten Million Dice\n") ;
>   Randomize() ;
> 
> >> seed the random number generator
> 
> for (i=3;i<=18;i++)
>   totals[ i ]=0;
> 
> >> Make sure all legal values (3-18) start from zero
> 
> for (i=0;i< tenmillion;i++) {
> 
> >> loop 10 Million times
> 
>   int d1=Random(6) ;
>   int d2=Random(6) ;
>   int d3=Random(6) ;
> 
> >> Get a random number from 1 to 6 for each of three die d1, d2, d3
> 
>   int total=d1+d2+d3;
> 
> >> Get the total amount of the roll
> 
>   totals[ total ]++;
> 
> >> Add 1 to the total counter.  i.e. if you rolled 6, 3, 2 the total
> >> will equal 11, so add one to total number 11.  This creates what is
> >> known as a histogram.
> }
> 
> for (i=3;i<=18;i++) {
>   printf("%i %i\n\r",i,totals[ i ]) ;
> }
> 
> >> Print the results in the form of "# total" for all legal values
> >> (3-18)
> 
>   return 0;
> 
> >> all functions in C return an integer value by default, so return
> >> 0.  Since this is the controlling function, the 0 will be returned
> >> to the OS as the errorlevel.  With error checking, you could have
> >> returned a 1 on error, and checked this value in your bash script.
> }
> 
> On Tue, 2008-02-05 at 09:08 -0700, Jim Hutchinson wrote:
> > In my attempts to learn about RNGs and programming tools, I stumbled
> > upon a little program apparently written in C. How do I compile or run
> > this in Ubuntu? Anyone know how to change it from 3 dice to 2? It's
> > mostly gibberish to me.
> > 
> > http://cplus.about.com/od/thecden/a/dicerolls.htm
> > 
> > Thanks,
> > Jim
> > 
> > -- 
> > Please avoid sending me Word or PowerPoint attachments.
> > See http://www.gnu.org/philosophy/no-word-attachments.html
> > 
> -- 
> Kevin Fries
> Senior Linux Engineer
> Computer and Communications Technology, Inc
> A Division of Japan Communications Inc.
> 
-- 
Kevin Fries
Senior Linux Engineer
Computer and Communications Technology, Inc
A Division of Japan Communications Inc.



More information about the Ubuntu-us-co mailing list