added random number generating function and ensure capacity for dynamic buffer