diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index 6cacea2b0..7391b215b 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -4,9 +4,15 @@ //////////////////////////////////////////////////////////// #include "Effect.hpp" #include -#include +#include +namespace +{ + std::random_device rd; + std::mt19937 rng(rd()); +} + const sf::Font* Effect::s_font = nullptr; //////////////////////////////////////////////////////////// @@ -135,15 +141,19 @@ public: bool onLoad() override { + std::uniform_real_distribution x_distribution(0, 800); + std::uniform_real_distribution y_distribution(0, 600); + std::uniform_int_distribution color_distribution(0, 255); + // Create the points m_points.setPrimitiveType(sf::Points); for (int i = 0; i < 40000; ++i) { - auto x = static_cast(std::rand() % 800); - auto y = static_cast(std::rand() % 600); - auto r = static_cast(std::rand() % 255); - auto g = static_cast(std::rand() % 255); - auto b = static_cast(std::rand() % 255); + auto x = x_distribution(rng); + auto y = y_distribution(rng); + auto r = static_cast(color_distribution(rng)); + auto g = static_cast(color_distribution(rng)); + auto b = static_cast(color_distribution(rng)); m_points.append(sf::Vertex(sf::Vector2f(x, y), sf::Color(r, g, b))); } diff --git a/examples/tennis/Tennis.cpp b/examples/tennis/Tennis.cpp index abb3e0eeb..c49c3c81a 100644 --- a/examples/tennis/Tennis.cpp +++ b/examples/tennis/Tennis.cpp @@ -4,9 +4,7 @@ //////////////////////////////////////////////////////////// #include #include -#include -#include -#include +#include #ifdef SFML_SYSTEM_IOS #include @@ -29,7 +27,8 @@ std::filesystem::path resourcesDir() //////////////////////////////////////////////////////////// int main() { - std::srand(static_cast(std::time(nullptr))); + std::random_device rd; + std::mt19937 rng(rd()); // Define some constants const float gameWidth = 800; @@ -140,7 +139,7 @@ int main() do { // Make sure the ball initial angle is not too much vertical - ballAngle = sf::degrees(static_cast(std::rand() % 360)); + ballAngle = sf::degrees(std::uniform_real_distribution(0, 360)(rng)); } while (std::abs(std::cos(ballAngle.asRadians())) < 0.7f); } @@ -231,6 +230,8 @@ int main() ball.setPosition({ball.getPosition().x, gameHeight - ballRadius - 0.1f}); } + std::uniform_real_distribution dist(0, 20); + // Check the collisions between the ball and the paddles // Left Paddle if (ball.getPosition().x - ballRadius < leftPaddle.getPosition().x + paddleSize.x / 2 && @@ -239,9 +240,9 @@ int main() ball.getPosition().y - ballRadius <= leftPaddle.getPosition().y + paddleSize.y / 2) { if (ball.getPosition().y > leftPaddle.getPosition().y) - ballAngle = sf::degrees(180) - ballAngle + sf::degrees(static_cast(std::rand() % 20)); + ballAngle = sf::degrees(180) - ballAngle + sf::degrees(dist(rng)); else - ballAngle = sf::degrees(180) - ballAngle - sf::degrees(static_cast(std::rand() % 20)); + ballAngle = sf::degrees(180) - ballAngle - sf::degrees(dist(rng)); ballSound.play(); ball.setPosition({leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y}); @@ -254,9 +255,9 @@ int main() ball.getPosition().y - ballRadius <= rightPaddle.getPosition().y + paddleSize.y / 2) { if (ball.getPosition().y > rightPaddle.getPosition().y) - ballAngle = sf::degrees(180) - ballAngle + sf::degrees(static_cast(std::rand() % 20)); + ballAngle = sf::degrees(180) - ballAngle + sf::degrees(dist(rng)); else - ballAngle = sf::degrees(180) - ballAngle - sf::degrees(static_cast(std::rand() % 20)); + ballAngle = sf::degrees(180) - ballAngle - sf::degrees(dist(rng)); ballSound.play(); ball.setPosition({rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y});