Browse Source

Import for Random

Kajetan Johannes Hammerle 11 months ago
parent
commit
9964a837ae
3 changed files with 59 additions and 49 deletions
  1. 10 0
      include/core/Random.h
  2. 16 16
      src/Random.c
  3. 33 33
      test/modules/RandomTests.c

+ 10 - 0
include/core/Random.h

@@ -15,4 +15,14 @@ size_t coreRandomSize(CoreRandom* r, size_t min, size_t exclusiveMax);
 bool coreRandomBool(CoreRandom* r);
 float coreRandomFloat(CoreRandom* r);
 
+#ifdef IMPORT_CORE
+#define Random CoreRandom
+#define initRandom coreInitRandom
+#define randomU32 coreRandomU32
+#define randomI32 coreRandomI32
+#define randomSize coreRandomSize
+#define randomBool coreRandomBool
+#define randomFloat coreRandomFloat
+#endif
+
 #endif

+ 16 - 16
src/Random.c

@@ -2,17 +2,17 @@
 
 static const size_t M = 7;
 
-void coreInitRandom(CoreRandom* r, u32 seed) {
+void initRandom(Random* r, u32 seed) {
     r->index = 0;
-    for(size_t i = 0; i < CORE_ARRAY_LENGTH(r->data); i++) {
+    for(size_t i = 0; i < ARRAY_LENGTH(r->data); i++) {
         r->data[i] = seed;
         seed = seed * 7 + 31;
     }
 }
 
-static void update(CoreRandom* r) {
+static void update(Random* r) {
     static const u32 map[2] = {0, 0x8EBFD028};
-    static const size_t LENGTH = CORE_ARRAY_LENGTH(r->data);
+    static const size_t LENGTH = ARRAY_LENGTH(r->data);
     for(size_t i = 0; i < LENGTH - M; i++) {
         r->data[i] = r->data[i + M] ^ (r->data[i] >> 1) ^ map[r->data[i] & 1];
     }
@@ -23,10 +23,10 @@ static void update(CoreRandom* r) {
     r->index = 0;
 }
 
-#define CORE_LIMIT(value, min, max) (min + value % (max - min))
+#define LIMIT(value, min, max) (min + value % (max - min))
 
-static u32 next(CoreRandom* r) {
-    if(r->index >= CORE_ARRAY_LENGTH(r->data)) {
+static u32 next(Random* r) {
+    if(r->index >= ARRAY_LENGTH(r->data)) {
         update(r);
     }
     u32 u = r->data[r->index++];
@@ -36,27 +36,27 @@ static u32 next(CoreRandom* r) {
     return u;
 }
 
-u32 coreRandomU32(CoreRandom* r, u32 min, u32 exclusiveMax) {
+u32 randomU32(Random* r, u32 min, u32 exclusiveMax) {
     u32 u = next(r);
-    return CORE_LIMIT(u, min, exclusiveMax);
+    return LIMIT(u, min, exclusiveMax);
 }
 
-i32 coreRandomI32(CoreRandom* r, i32 min, i32 exclusiveMax) {
+i32 randomI32(Random* r, i32 min, i32 exclusiveMax) {
     i32 i = (i32)(next(r) >> 1);
-    return CORE_LIMIT(i, min, exclusiveMax);
+    return LIMIT(i, min, exclusiveMax);
 }
 
-size_t coreRandomSize(CoreRandom* r, size_t min, size_t exclusiveMax) {
+size_t randomSize(Random* r, size_t min, size_t exclusiveMax) {
     size_t s = next(r);
-    return CORE_LIMIT(s, min, exclusiveMax);
+    return LIMIT(s, min, exclusiveMax);
 }
 
-bool coreRandomBool(CoreRandom* r) {
+bool randomBool(Random* r) {
     return next(r) & 1;
 }
 
-float coreRandomFloat(CoreRandom* r) {
+float randomFloat(Random* r) {
     u32 u = next(r);
     float f = (float)u / (float)0xFFFFFFF;
-    return f >= 1.0f ? coreRandomFloat(r) : f;
+    return f >= 1.0f ? randomFloat(r) : f;
 }

+ 33 - 33
test/modules/RandomTests.c

@@ -3,93 +3,93 @@
 
 static void testAverage(bool light) {
     int limit = light ? 100000 : 1000000;
-    CoreRandom r;
-    coreInitRandom(&r, 553);
+    Random r;
+    initRandom(&r, 553);
     float sum = 0;
     for(int i = 0; i < limit; i++) {
-        u32 u = coreRandomU32(&r, 2, 11);
+        u32 u = randomU32(&r, 2, 11);
         sum += (float)u;
     }
     sum /= (float)limit;
-    CORE_TEST_FLOAT(6.0f, sum, 0.01f);
+    TEST_FLOAT(6.0f, sum, 0.01f);
 }
 
 static void testCoin(bool light) {
     int limit = light ? 100000 : 1000000;
-    CoreRandom r;
-    coreInitRandom(&r, 5533);
+    Random r;
+    initRandom(&r, 5533);
     int c[2] = {0, 0};
     for(int i = 0; i < limit; i++) {
-        c[coreRandomBool(&r)]++;
+        c[randomBool(&r)]++;
     }
-    CORE_TEST_FLOAT(0.0f, (float)(c[0] - c[1]) / (float)limit, 0.003f);
+    TEST_FLOAT(0.0f, (float)(c[0] - c[1]) / (float)limit, 0.003f);
 }
 
 static void testDistribution(bool light) {
     size_t limit = light ? 100000 : 1000000;
-    CoreRandom r;
-    coreInitRandom(&r, 553);
+    Random r;
+    initRandom(&r, 553);
     int c[102] = {0};
     for(size_t i = 0; i < limit; i++) {
-        c[coreRandomSize(&r, 1, 101)]++;
+        c[randomSize(&r, 1, 101)]++;
     }
-    CORE_TEST_INT(0, c[0]);
-    CORE_TEST_INT(0, c[101]);
+    TEST_INT(0, c[0]);
+    TEST_INT(0, c[101]);
     for(size_t i = 1; i < 101; i++) {
-        CORE_TEST_FLOAT(0.01f, (float)c[i] / (float)limit, 0.001f);
+        TEST_FLOAT(0.01f, (float)c[i] / (float)limit, 0.001f);
     }
 }
 
 static void testFloatAverage(bool light) {
     int limit = light ? 100000 : 1000000;
-    CoreRandom r;
-    coreInitRandom(&r, 553);
+    Random r;
+    initRandom(&r, 553);
     float sum = 0;
     for(int i = 0; i < limit; i++) {
-        sum += coreRandomFloat(&r);
+        sum += randomFloat(&r);
     }
     sum /= (float)limit;
-    CORE_TEST_FLOAT(0.5f, sum, 0.001f);
+    TEST_FLOAT(0.5f, sum, 0.001f);
 }
 
 static void testFloatCoin(bool light) {
     int limit = light ? 100000 : 1000000;
-    CoreRandom r;
-    coreInitRandom(&r, 5534);
+    Random r;
+    initRandom(&r, 5534);
     int c[2] = {0, 0};
     for(int i = 0; i < limit; i++) {
-        c[coreRandomFloat(&r) < 0.5f]++;
+        c[randomFloat(&r) < 0.5f]++;
     }
-    CORE_TEST_FLOAT(0.0f, (float)(c[0] - c[1]) / (float)limit, 0.003f);
+    TEST_FLOAT(0.0f, (float)(c[0] - c[1]) / (float)limit, 0.003f);
 }
 
 static void testFloatDistribution(bool light) {
     int limit = light ? 100000 : 1000000;
-    CoreRandom r;
-    coreInitRandom(&r, 553);
+    Random r;
+    initRandom(&r, 553);
     int c[102] = {0};
     for(int i = 0; i < limit; i++) {
-        c[(size_t)(1.0f + coreRandomFloat(&r) * 100.0f)]++;
+        c[(size_t)(1.0f + randomFloat(&r) * 100.0f)]++;
     }
-    CORE_TEST_INT(0, c[0]);
-    CORE_TEST_INT(0, c[101]);
+    TEST_INT(0, c[0]);
+    TEST_INT(0, c[101]);
     for(size_t i = 1; i < 101; i++) {
-        CORE_TEST_FLOAT(0.01f, (float)c[i] / (float)limit, 0.003f);
+        TEST_FLOAT(0.01f, (float)c[i] / (float)limit, 0.003f);
     }
 }
 
 static void testRandomI32() {
-    CoreRandom r;
-    coreInitRandom(&r, 56346);
+    Random r;
+    initRandom(&r, 56346);
     int c[7] = {0};
     for(int i = 0; i < 10000; i++) {
-        i32 index = coreRandomI32(&r, -2, 3) + 3;
-        if(CORE_TEST_TRUE(index >= 0 && index < 7)) {
+        i32 index = randomI32(&r, -2, 3) + 3;
+        if(TEST_TRUE(index >= 0 && index < 7)) {
             c[index]++;
         }
     }
     for(size_t i = 0; i < 7; i++) {
-        CORE_TEST_BOOL(i != 0 && i != 6, c[i] > 0);
+        TEST_BOOL(i != 0 && i != 6, c[i] > 0);
     }
 }