On Nov 27, 9:57 am, Eivind Eklund <eekl... / gmail.com> wrote: > If you're going to do overkill, do overkill. You've got an extra > compare in that main loop, with the check for n-- > 0. I assume you're just having fun, but even so, if your compiler doesn't optimize (n-- > 0) to be as efficient as (n--), get a new compiler. > And it doesn't > work for negative numbers. Correcting for this (untested): Yes, rolling a negative number of dice is precluded, it just confuses people. > int roll(int n) { > int sum = n; > if (n > 0) { > do { > sum += rand() % 6; > } while (--n); > } else if (n < 0) { > do { > sum -= rand() % 6; > } while (++n); > } > return sum; > > } You might want to double check that code... > Of course, this still has the problem that rand() % 6 typically return > slightly biased numbers, as RAND_MAX is usually a 2^n-1 Ok, here you have a point. The roll function won't cut it in Las Vegas. > ;-) > > Eivind.