From 6b1ba02982113dbfe6c8975ffed4ecc91b6db54e Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 23 Oct 2017 19:08:00 +0100 Subject: [PATCH] Ensured Random::Range does not overflow If rand() & 0xfff is equal to 0xfff, then the range computation would overflow past max value due to float inaccuracy. Changed the order of terms to prevent overflow (whilst retaining compile time division). Change-Id: I286e0b5c31438f32f59e53dc7a8745745369d16e Signed-off-by: David Steele --- dali/public-api/math/random.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dali/public-api/math/random.h b/dali/public-api/math/random.h index 2e8391e..2d5d237 100644 --- a/dali/public-api/math/random.h +++ b/dali/public-api/math/random.h @@ -2,7 +2,7 @@ #define __DALI_RANDOM_H__ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2017 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,11 +54,13 @@ inline float Range(float f0, float f1) static bool initialized( false ); if( !initialized ) { - srand( time( NULL ) ); + auto seed = time( NULL ); + srand( seed ); initialized = true; } - return min + (rand() & 0xfff) * (max-min) * (1.0f/4095.0f); + auto randValue = rand(); + return (randValue & 0xfff) * (1.0f/4095.0f) * (max-min) + min; } /** -- 2.7.4