#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
typedef struct {
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
} time_b ;
const int SIDES=6 ;
const int NUMDICE=4 ;
comp (const void *i, const void *j)
{
return *(int *)i - *(int *)j ;
}
void main(void)
{
int dice[NUMDICE] ;
int i, total, final_total=0, stime ;
long ltime, z ;
time_b ft ;
float average, final_average ;
for (z=0; z<=1000000; z++)
{
ltime = time(NULL) ;
stime = (unsigned) ltime/2 ;
(void)ftime(&ft) ;
srand((int) ft.millitm + stime) ;
for (i=0; i<=NUMDICE-1; i++)
{
dice = (rand() % SIDES) +1 ;
}
qsort(dice, NUMDICE, sizeof(int), comp) ;
total=0 ;
average=0 ;
for (i=NUMDICE-3; i<=NUMDICE-1; i++)
{
total+=dice ;
}
average = (float) total/(NUMDICE-1) ;
final_total+=total ;
final_average = (float) final_total/z ;
}
printf("final total = %d\n", final_total) ;
printf("final average = %f\n", final_average) ;
}
Heh... I needed to read further than skimming. What you've done is to define, mathematically, a brute-force algorithm for determining the correct result. Which is, I'll admit, pretty clever... but that's not what we're trying to do. We want to do this without all the iterations; theoretically, per die we should be able to boil things down to half the number of iterations or less (so, for 10 dice, 1/1024 = 1/2^10 or less iterations than running things for every possible roll).
What the equation does is run through all possible sorted combinations of the kept dice that add up to the specified value, and figures out the number of combinations that result in that sorted set of kept dice, sums them all up, and divides by total possible combinations. Where the trick comes in is that figuring out the number of combinations requires a multinomial coefficient, but the inputs to the multinomial coefficient depend on how many and which values on the kept dice are equal to each other. That's what the c_j's calculate.
LazarusLong42 said:Would you be willing to post the spreadsheet as well? I've been trying to deduce symbolic representations from patterns in the combinatorics, but couldn't find a pattern in anything dropping more than one die.
LazarusLong42 said:
Heh... I needed to read further than skimming. What you've done is to define, mathematically, a brute-force algorithm for determining the correct result.
dylanarena said:Nathan, I built the function based on your analysis, so thank you very much!

(Dungeons & Dragons)
Rulebook featuring "high magic" options, including a host of new spells.