We consider a market where a finite number of players trade an asset whose supply is a stochastic process. The price formation problem consists of finding a price process that ensures that when agents act optimally to minimize their trading costs, the market clears, and supply meets demand. This problem arises in market economies, including electricity generation from renewable sources in smart grids. Our model includes noise on the supply side, which is counterbalanced on the consumption side by storing energy or reducing the demand according to a dynamic price process. By solving a constrained minimization problem, we prove that the Lagrange multiplier corresponding to the market-clearing condition defines the solution of the price formation problem. For the linear-quadratic structure, we characterize the price process of a continuum population using optimal control techniques. We include numerical schemes for the price computation in the finite and infinite games, and we illustrate the model using real data.