Moved random initialization of weights into a function.