From 6954aaeccfaf962c31ef5f1c9a5d62b14df1537f Mon Sep 17 00:00:00 2001 From: lynxeyed-atsu Date: Wed, 27 Jun 2012 21:48:25 +0900 Subject: added Random function --- src/core/arithmetical.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/core/arithmetical.h | 11 +++++++++++ 2 files changed, 48 insertions(+) create mode 100755 src/core/arithmetical.cpp (limited to 'src') diff --git a/src/core/arithmetical.cpp b/src/core/arithmetical.cpp new file mode 100755 index 0000000..ad90612 --- /dev/null +++ b/src/core/arithmetical.cpp @@ -0,0 +1,37 @@ +#include +#include + +unsigned int random_seed; + +void randomSeed(unsigned int seed) +{ + random_seed = seed; + return; +} + + +// random algorithm using Fibonacci-LFSR +unsigned int random(void) +{ + unsigned int shift_bit; + shift_bit = (random_seed & 0x0001) ^ + ((random_seed & 0x0004) >> 2) ^ + ((random_seed & 0x0008) >> 3) ^ + ((random_seed & 0x0020) >> 5); + random_seed = (random_seed >> 1) | (shift_bit << 15); + return random_seed; +} + +long random(long max_num) +{ + if(max_num ==0) return 0; + return random() % max_num; +} + +long random(long min_num, long max_num) +{ + if(min_num >= max_num)return 0; + if(max_num == 0) return 0; + + return random( max_num - min_num ) + min_num; +} diff --git a/src/core/arithmetical.h b/src/core/arithmetical.h index bae4440..a087807 100755 --- a/src/core/arithmetical.h +++ b/src/core/arithmetical.h @@ -45,5 +45,16 @@ #define map(x,in_min,in_max,out_min,out_max) \ (((x) - (in_min)) * ((out_max) - (out_min)) / ((in_max) - (in_min)) + (out_min)) +#ifdef __cplusplus + extern "C++" { +#endif + +void randomSeed(unsigned int seed); +unsigned int random(void); +long random(long max_num); +long random(long min_num, long max_num); +#ifdef __cplusplus + } +#endif #endif /* ARITHMETICAL_H_ */ -- cgit v1.2.3