fix reference error
add random number generation