add a template based version for faster computation, fix seed 0