Optimized sampling functions
Fixed problems with MSVC