Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Schuetz2014-06-25 09:07:58 +0000
committerThomas Schuetz2014-06-25 09:07:58 +0000
commit7b24fc19dc000f23f00c04b205c4e6d90c2f6e4b (patch)
tree9aee9202b0d50658a5b70f89165ceae230e1709b /runtime/org.eclipse.etrice.runtime.c/src/util
parentab7f7e23d6ac7eb57ebec017f4328766e3da5501 (diff)
downloadorg.eclipse.etrice-7b24fc19dc000f23f00c04b205c4e6d90c2f6e4b.tar.gz
org.eclipse.etrice-7b24fc19dc000f23f00c04b205c4e6d90c2f6e4b.tar.xz
org.eclipse.etrice-7b24fc19dc000f23f00c04b205c4e6d90c2f6e4b.zip
[runtime.c] added util folder with RandomGenerator and added testcase
Diffstat (limited to 'runtime/org.eclipse.etrice.runtime.c/src/util')
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.c34
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.h54
2 files changed, 88 insertions, 0 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.c b/runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.c
new file mode 100644
index 000000000..d849b4edb
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.c
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2014 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Thomas Schuetz (initial contribution)
+ *
+ *******************************************************************************/
+
+#include "RandomGenerator.h"
+
+#include "math.h"
+
+void RandomGenerator_init(RandomGenerator* self, float64 seed, float64 min, float64 max){
+ self->seed = seed;
+ self->min = min;
+ self->max = max;
+ self->range = max-min;
+ self->current = seed;
+}
+
+
+float64 RandomGenerator_getNext(RandomGenerator* self){
+ self->seed = 1000. * fabs(log(self->seed)); /* shift comma 3 times to get nice values */
+ int64 resultInt = self->seed; /* get int number left of comma */
+ self->seed = self->seed - resultInt; /* cut off everything left of comma */
+ self->current = self->seed*self->range + self->min; /* stretch to range */
+ self->seed += 0.1; /** seed must always be bigger than 0 for log in next iteration */
+
+ return self->current;
+}
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.h b/runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.h
new file mode 100644
index 000000000..3cda64a74
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.c/src/util/RandomGenerator.h
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2014 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Thomas Schuetz (initial contribution)
+ *
+ *******************************************************************************/
+
+#ifndef RANDOMGENERATOR_H_
+#define RANDOMGENERATOR_H_
+
+#include "etDatatypes.h"
+
+typedef struct RandomGenerator {
+ float64 seed;
+ float64 max;
+ float64 min;
+ float64 range;
+ float64 current;
+}RandomGenerator;
+
+/**
+* initialze once before use.
+*
+* @param self self pointer of class.
+* @param seed seed for the random function. Must be ]0,*] - should be ]0,1[ for a good start
+* @param min minimum for value range of resulting numbers
+* @param max maximum for value range of resulting numbers
+*
+* example:
+* (...)
+* RandomNumberGenerator rand;
+* RandomNumberGenerator_init(&rand, 0.321, -10, 10);
+* float64 result = RandomNumberGenerator_getNext(&rand);
+* (...)
+* result now holds a random number between -10 and 10;
+*/
+void RandomGenerator_init(RandomGenerator* self, float64 seed, float64 min, float64 max);
+
+/**
+* initialze once before use.
+*
+* @param self self pointer of class.
+* @return A random number between min and max
+*
+*/
+float64 RandomGenerator_getNext(RandomGenerator* self);
+
+
+#endif /* RANDOMGENERATOR_H_ */

Back to the top