This paper presents a novel approach to solve the optimal power flow (OPF) problem by utilizing a modified white shark optimization (MWSO) algorithm. The MWSO algorithm incorporates the Gaussian barebones (GB) and quasi-oppositional-based learning (QOBL) strategies to improve the convergence rate and accuracy of the original WSO algorithm. To address the uncertainty associated with renewable energy sources, the IEEE 30 bus system, which consists of 30 buses, 6 thermal generators, and 41 branches, is modified by replacing three thermal generators with two wind generators and one solar PV generator. And the IEEE 57-bus system, which consists of 57 buses, 7 thermal generators, and 80 branches, is also modified by the same concept. The variability of wind and solar generation is described using the Weibull and lognormal distributions, and its impact on the OPF problem is considered by incorporating reserve and penalty costs for overestimation and underestimation of power output. The paper also takes into account the unpredictability of power consumption (load demand) by analyzing its influence using standard probability density functions (PDF). Furthermore, practical conditions related to the thermal generators, such as ramp rate limits are examined. The MWSO algorithm is evaluated and analyzed using 23 standard benchmark functions, and a comparative study is conducted against six well-known techniques using various statistical parameters. The results and statistical analysis demonstrate the superiority and effectiveness of the MWSO algorithm compared to the original WSO algorithm for addressing the OPF problem in the presence of generation and demand uncertainties.