aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlynxeyed-atsu <lynxeyed@xj9.so-net.ne.jp>2012-06-27 21:48:25 +0900
committerlynxeyed-atsu <lynxeyed@xj9.so-net.ne.jp>2012-06-27 21:48:25 +0900
commit6954aaeccfaf962c31ef5f1c9a5d62b14df1537f (patch)
treec2cebb7f53f37dccdc4bce91b9712fa8aeae8325 /src
parent5866ec963c8c90fc25febf9814c2dd218478df75 (diff)
added Random function
Diffstat (limited to 'src')
-rwxr-xr-xsrc/core/arithmetical.cpp37
-rwxr-xr-xsrc/core/arithmetical.h11
2 files changed, 48 insertions, 0 deletions
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 <libmary.h>
+#include <arithmetical.h>
+
+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_ */