The optimal behaviour of firms facing stochastic costs