On Nov 27, 2007 3:35 PM, Brian Adkins <lojicdotcom / gmail.com> wrote: > Why be satisfied with a 10-12% increase in speed when we can have an > order of magnitude? :) > > sudo gem install rubyinline > > -- > > require 'rubygems' > require 'inline' > > module Kernel > inline do |builder| > builder.c " > int roll(int n) { > int sum = n; > while (n-- > 0) { > sum += (rand() % 6); > } > return sum; > } > " > end > end > > puts roll(3) 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. And it doesn't work for negative numbers. Correcting for this (untested): 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; } Of course, this still has the problem that rand() % 6 typically return slightly biased numbers, as RAND_MAX is usually a 2^n-1 ;-) Eivind.