This paper introduces an efficient Monte Carlo algorithm for calculating the density of states, which is crucial for studying phase transitions and complex systems. The method involves performing multiple random walks in different energy ranges, adjusting the density of states continuously to produce flat histograms. This approach allows for accurate calculations of thermodynamic quantities like free energy and entropy without relying on temperature. The algorithm is effective for both first-order and second-order phase transitions and is particularly useful for systems with rough energy landscapes. The method was tested on the 2D Ising model and the 2D ten-state Potts model, demonstrating high accuracy and efficiency. The results showed minimal errors in the density of states, free energy, and specific heat, even for large systems. The algorithm's ability to overcome tunneling barriers and its independence from temperature make it a powerful tool for studying complex systems in statistical physics.This paper introduces an efficient Monte Carlo algorithm for calculating the density of states, which is crucial for studying phase transitions and complex systems. The method involves performing multiple random walks in different energy ranges, adjusting the density of states continuously to produce flat histograms. This approach allows for accurate calculations of thermodynamic quantities like free energy and entropy without relying on temperature. The algorithm is effective for both first-order and second-order phase transitions and is particularly useful for systems with rough energy landscapes. The method was tested on the 2D Ising model and the 2D ten-state Potts model, demonstrating high accuracy and efficiency. The results showed minimal errors in the density of states, free energy, and specific heat, even for large systems. The algorithm's ability to overcome tunneling barriers and its independence from temperature make it a powerful tool for studying complex systems in statistical physics.