updated function and added random gradient descent