Separate random number generating function out of memtest.c

Also refine code readability in memtest.c during modification.