The recent expansion of networks of low-earth orbit (LEO) satellites such as Starlink, OneWeb, and Telesat and the evolution of communication systems toward B5G and 6G with densely interconnected devices could generate opportunities for various cyber attacks. As the satellite network offers many crucial services to the public and governmental organizations, cyberattacks pose severe risks to the communication infrastructure. In this study, we propose a random routing algorithm to prevent distributed denial-of-service (DDoS) attacks on an LEO satellite constellation network. The routing algorithm utilizes the classical algorithms, i.e., k-DG, k-DS, k-SP, and k-LO, by introducing randomness and selecting one with weighted probability distribution to increase the uncertainty in the algorithm. The study shows that the proposed random routing algorithm improves the average and median cost of the attacker against DDoS attacks while maintaining the functionality of the network. The algorithm is optimized by formulating a Bayesian optimization problem. In addition to providing an additional level of uncertainty in the routing, there is an improvement of 1.71% in the average cost and 2.05% in the median cost in a typical scenario. The algorithm causes the network to be robust to cyber attacks against LEO Satellite Networks (LSNs), however, similar to any other defensive measures, it reduces the network’s goodput.