rand func and refactor