From 2fcecaa7c5f3a42fdc23f2b83c193c0706708192 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 12 Apr 2017 21:16:07 +1000 Subject: [PATCH] AP_Math: added rand_float() --- libraries/AP_Math/AP_Math.cpp | 8 ++++++++ libraries/AP_Math/AP_Math.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/libraries/AP_Math/AP_Math.cpp b/libraries/AP_Math/AP_Math.cpp index 4476aec439..1b17bbf304 100644 --- a/libraries/AP_Math/AP_Math.cpp +++ b/libraries/AP_Math/AP_Math.cpp @@ -211,3 +211,11 @@ uint16_t get_random16(void) return ((m_z << 16) + m_w) & 0xFFFF; } + +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL +// generate a random float between -1 and 1, for use in SITL +float rand_float(void) +{ + return ((((unsigned)random()) % 2000000) - 1.0e6) / 1.0e6; +} +#endif diff --git a/libraries/AP_Math/AP_Math.h b/libraries/AP_Math/AP_Math.h index 2969e10577..b3ad2ad106 100644 --- a/libraries/AP_Math/AP_Math.h +++ b/libraries/AP_Math/AP_Math.h @@ -221,3 +221,6 @@ float linear_interpolate(float low_output, float high_output, /* simple 16 bit random number generator */ uint16_t get_random16(void); +// generate a random float between -1 and 1, for use in SITL +float rand_float(void); +