Svip Posted February 21, 2006 Share Posted February 21, 2006 I have written an application in C (which sources you will see in the bottom of this post). It simply rolls a N eyed dice a number of times, however it gives you a bit of features, and I hope to add more soon. However, this applications may not work under Windows (unless using the prompt), since it works like another UNIX command (but I have not worked with DOS commands that much to know their nature). At default, no 'input' is required, however it can be given, or as the help file reads: [email protected]:~/prog$ dice helpdice version 1.0Usage: dice [--force-row] [number of rolls [sides on dice] ] dice [help|-h]--force-row Use to display the row of the rollings for reference, normally this information is not displayed.Standard values:Rolls: 1000, Eyes: 6 Of cause help|-h displays that help and terminates the application. As you can read there are standard values; it will roll 1000 times and use a six eyed dice on default. Example: [email protected]:~/prog$ diceRolling 6 eyed dice; 1000 times... Done!Number of eyes table:1 : 1722 : 1713 : 1694 : 1695 : 1436 : 176Logical Average: 166.666672 The logical average is a float variable calculated from rolls/eyes. The results are always different (not yet have I encounted alike results). But the rolls and eyes can easily be changed. Example: [email protected]:~/prog$ dice 525 13Rolling 13 eyed dice; 525 times... Done!Number of eyes table:1 : 442 : 433 : 404 : 385 : 516 : 347 : 408 : 469 : 4710 : 2811 : 3912 : 3213 : 43Logical Average: 40.384617 In case you want to see the original row the rolls was returned to generate the final result, you can use --force-row in front of the 'rollings' and 'eyes' in the command (of course those two are not required when using --force-row). --force-row will display the information that is hidden on default cause it can be very long. Example: [email protected]:~/prog$ dice --force-row 200 3Rolling 3 eyed dice; 200 times...--force-row is turned on, displaying all the results of each roll as they come.2 3 3 3 2 1 3 1 3 3 3 1 2 2 2 1 2 2 2 1 3 3 1 3 2 2 1 3 2 3 1 3 3 3 3 1 1 2 1 32 1 3 3 3 2 1 1 1 3 2 3 3 2 3 1 2 3 2 3 3 2 2 3 2 2 3 1 1 2 3 2 3 1 2 2 2 3 3 23 1 2 2 1 2 3 2 2 1 1 2 3 3 1 2 1 1 2 1 3 2 2 2 2 1 1 1 1 1 3 3 2 3 1 2 2 3 3 32 1 2 1 3 3 3 3 3 1 1 3 1 3 1 2 1 2 3 1 2 3 3 1 3 1 2 1 1 3 3 2 3 2 3 3 2 2 3 21 2 2 1 2 3 3 2 2 3 3 1 3 3 2 3 3 2 3 2 1 3 3 1 3 3 3 1 2 1 3 3 3 2 1 1 2 3 3 1Done!Number of eyes table:1 : 552 : 653 : 80Logical Average: 66.666664 As you can see, the applications has some small nifty features, and now for the source code. #include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#define TRUE 1//This definition is rather useless, I used it anyway.int get_rand(int j) {//This function simply returns a random value between 1 and j.//Though the rand() would return 0 to j-1, so I add 1 for this issue. return rand()%j + 1;}int game(int g, int l, int row) {//This function does the game entirely. int i,n; //i is used for counting, n is the random number from the function long a[g]; //a is our array, which has g elements, g being the amount of eyes float f, fg, fl; //float variables, it did not somehow work with f= l/g, so I made some floats up! for(i=0;i<g;i++) //This is perhaps not required, but I am using it //just in case it is. a[i] = 0; srand(time(NULL)); //Make sure that the random returns different values //from run to run. printf("Rolling %d eyed dice; %d times... ",g,l); if(row==TRUE) //If --force-row is on, tell the user. printf("\n--force-row is turned on, displaying all the results of each roll as they come.\n"); for(i=0;i<l;i++) { n = get_rand(g); //Get the random value from the function above if(row==TRUE) { //Show the user the roll if row is TRUE. printf("%d ",n); if(!((i+1) % 40)) printf("\n"); } a[(n-1)]++; //Add one to the element in the array (a). } if(row==TRUE) {printf("\n");} printf("Done!\n"); //Tell the user we have done the "calculations". printf("Number of eyes table:\n"); for(i=0;i<g;i++) //Print the table of results. printf("%d\t: %d\n",(i+1),a[i]); fl = l; fg = g; f = fl/fg; printf("Logical Average: %f\n",f); //A calculated average for reference.}int main(int argc, char *argv[]) { //This is the main function, it just make sures the application //goes in the right direction. static char buffer[20]; int p; p = 2; //p is 'like' a pointer, pointing at the 'word' in the input field. if(argc >= p) //Check if user have inputted anything otherwise //we would get a segfault. strcpy(buffer, argv[1]); if((!strcmp(buffer, "help")) || (!strcmp(buffer, "-h"))) { //Print help if user have typed "help" or "-h". printf("dice version 1.0\n\n"); printf("Usage:\n"); printf("\t%s [--force-row] [number of rolls [sides on dice] ]\n",argv[0]); printf("\t%s [help|-h]\n",argv[0]); printf("\n--force-row\n\tUse to display the row of the rollings for reference,\n\tnormally this information is not displayed.\n"); printf("\nStandard values:\n"); printf("Rolls: 1000, Eyes: 6\n"); } else { //If the user haven't asked for help, get the values he have inputted. int l,g,n; if(!strcmp(buffer, "--force-row")) { n = TRUE; p++; //Since the first 'word' is already used, we need to check the correct line next time. } if(argc < p) l = 1000; else { l = atoi(argv[(p-1)]); //Remember to make it a number from string, heh. p++; } if(argc < p) g = 6; else { g = atoi(argv[(p-1)]); //Same as above. p++; } game(g,l,n); //Run the game function! }} I hope you enjoy, I got the idea in physic class when our teacher said something about getting a computer to calculate how a dice would roll a thousand times. Link to comment Share on other sites More sharing options...
hawk Posted February 22, 2006 Share Posted February 22, 2006 Nice one svip, now just add some graphics and sell it to Microsoft Link to comment Share on other sites More sharing options...
Svip Posted February 22, 2006 Author Share Posted February 22, 2006 Nice one svip, now just add some graphics and sell it to Microsoft Err...? what?* Anyways, I am glad you liked it, hope more to think so as well. *Seriously, I cannot see what was funny in that post, it was wrong in so many ways. Link to comment Share on other sites More sharing options...
hawk Posted February 22, 2006 Share Posted February 22, 2006 I was at work, on my lunch. It seemed funny to me at the time. Now it just seems stupid. Link to comment Share on other sites More sharing options...
andibomb Posted February 22, 2006 Share Posted February 22, 2006 I actually find this program quite interesting, however pointless it may seem, due to my inexplicable interest with probability. Maybe you could add a Standard Deviation feature, showing the variation of the data from the average. Link to comment Share on other sites More sharing options...
Xyzar Posted February 23, 2006 Share Posted February 23, 2006 <!-- nice app, i guess. rather simple, but neat. (don't really see the point of the topic though) --> Link to comment Share on other sites More sharing options...
just another thug Posted February 24, 2006 Share Posted February 24, 2006 Very cool, we had to do something like this, only I wrote it in Java. It wasn't as complex but it had the same idea. Link to comment Share on other sites More sharing options...
K^2 Posted March 8, 2006 Share Posted March 8, 2006 (edited) To make f=l/g work in one line without dummy variables, you have to typecast the integers as floats: f = ((float)l)/((float)g); //This will give you a good floating point result with no truncation. Edit: And what is it supposed to be, anyways? Number of rolls divided by number of sides? What's the point? Wouldn't it make more sense to compute the actual average roll and a statistically predicted one? Statistical average, by the way, will allways be (g+1)/2, regardles of l. Edited March 8, 2006 by K^2 Prior to filing a bug against any of my code, please consider this response to common concerns. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now