Orthogonal variable spreading factor (OVSF) codes are widely used to provide variable data rates for supporting different bandwidth requirements in wideband code division multiple access (WCDMA) systems. Many works in the literature have intensively investigated to find an optimal dynamic code assignment scheme for OVSF codes. Unlike earlier studies, which assign OVSF codes using conventional (CCA) or dynamic (DCA) code allocation schemes, in this paper, stochastic optimization methods which are genetic algorithm (GA) and simulated annealing (SA) were applied which population is adaptively constructed according to existing traffic density in the OVSF code-tree. Simulation results show that the GA and SA provide reduced code blocking probability and improved spectral efficiency when compared to the CCA and DCA schemes. It is also seen that, computational complexity of GA and SA are also more than CCA and DCA.