From 9e54f238fbfced9243cc544c1461f21eefd5b0f1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 09:56:45 +0100 Subject: add Random.h --- game/CMakeLists.txt | 2 +- game/Random.cpp | 28 ++++++++++++++++++++++++++++ game/Random.h | 11 +++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 game/Random.cpp create mode 100644 game/Random.h (limited to 'game') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 661e2bc..c8fa989 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -4,7 +4,6 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_BUILD_TYPE Debug) - project(game C CXX) add_subdirectory(../src crepe) @@ -48,6 +47,7 @@ add_executable(main hud/HudSubScene.cpp hud/HudScript.cpp hud/SpeedScript.cpp + Random.cpp ) target_link_libraries(main PUBLIC crepe) diff --git a/game/Random.cpp b/game/Random.cpp new file mode 100644 index 0000000..59be3c5 --- /dev/null +++ b/game/Random.cpp @@ -0,0 +1,28 @@ +#include + +#include "Random.h" + +float Random::f(float upper, float lower) { + float range = upper - lower; + float x = ((float) rand() / (float) (RAND_MAX)) * range; + return x + lower; +} + +double Random::d(double upper, double lower) { + double range = upper - lower; + double x = ((double) rand() / (double) (RAND_MAX)) * range; + return x + lower; +} + +int Random::i(int upper, int lower) { + int range = upper - lower; + int x = rand() % range; + return x + lower; +} + +unsigned Random::u(unsigned upper, unsigned lower) { + unsigned range = upper - lower; + unsigned x = rand() % range; + return x + lower; +} + diff --git a/game/Random.h b/game/Random.h new file mode 100644 index 0000000..cf05e87 --- /dev/null +++ b/game/Random.h @@ -0,0 +1,11 @@ +#pragma once + +class Random { +public: + static float f(float upper = 1.0, float lower = 0.0); + static double d(double upper = 1.0, double lower = 0.0); + static int i(int upper, int lower = 0); + static unsigned u(unsigned upper, unsigned lower = 0); + +}; + -- cgit v1.2.3