A while back I wrote a small program to simulate dice rolls. It was a simple program, all it did was call the rand() function to retrieve a number between 1 and 6 and add the rolls together then subtract the lowest roll.
| CODE |
unsigned int rolls[4], temp, count;
for (count = 0; count < 4; count++) { rolls[count] = rand() % 6 + 1; } for (count = 0; count < 3; count++) { if (rolls[count] < rolls[count+1]) { temp = rolls[count+1]; rolls[count+1] = rolls[count]; rolls[count] = temp; } } return rolls[0] + rolls[1] + rolls[2]; |
This worked great, but I began thinking is there a better way to do this. After a bit of though I decided to enumerate the rolls (basically count the possibilities of landing on each number). I then took this table and tried to find an algorithm but because of the way the rolls came out there really wasn’t any that I could find they had an unusual shape when graphed and with my current mathematic ability finding one outside my mathematical ability if it's even possible. So my next idea was to create an array and have the program loop through calling rand() once. But I decided against this even though i would be limiting the math used I would be adding a loop which could possibly slow down the speed of the program. Finally I sealed on rolling out the loop using a series of checks to do the process. I decided to do this in inline asm because doing it in C++ would violate one of the first rules I learned in C++ (never use the GOTO statement). when i was done i tested the results and found that in Visual C++ (my compiler) that i had cut the time it took to run the roll simulation was less than half of the time it took to just run the simulation. this may all seem redundant and useless but this is a process I was planning on using for a server / CGI app that may end up being run thousands of times a second. and having it run at 2x the speed would have a noticeable difference.
this may not apply to all situations and I know I fell back on asm but the main speed of this comes from using a better algorithm. The main reason for this speed increase is because the most taxing thing your computer can do is division there is nothing as slow. and the "rand() % 6 + 1" is basically call rand divide by 6 then ad 1 to the remainder. then i had to run a second loop to find the lowest number. And remove it from the result. by cutting the divisions down to 1 and eliminating the second loop altogether I gained a major speed increase.
For anyone who cares here is the inline asm
| CODE |
_asm { call rand mov ecx, 1296 cdq div ecx inc edx mov eax, 18 cmp edx, 1275 jg rollend dec eax //17 cmp edx, 1221 jg rollend dec eax //16 cmp edx, 1127 jg rollend dec eax //15 cmp edx, 996 jg rollend dec eax //14 cmp edx, 836 jg rollend dec eax //13 cmp edx, 664 jg rollend dec eax //12 cmp edx, 497 jg rollend dec eax //11 cmp edx, 349 jg rollend dec eax //10 cmp edx, 227 jg rollend dec eax //9 cmp edx, 136 jg rollend dec eax //8 cmp edx, 74 jg rollend dec eax //7 cmp edx, 36 jg rollend dec eax //6 cmp edx, 15 jg rollend dec eax //5 cmp edx, 5 jg rollend dec eax //4 cmp edx, 1 jg rollend dec eax //3 rollend: ret } |
Sorry, for babbling I just wanted to state the importance of better algorithms.
Hmm, i think this might be a little too advanced for most here. either that or no one understands it lol. i was hoping to see some algorithm alternitives here but i guess i wont -_-
I'd comment if it weren't for the fact that I don't have internet connection on my laptop atm :( Am back home, need to fix the damn home computer so the internet connection sharing actually work! :(