소스 검색

ADDED: `LoadRandomSequence()`/`UnloadRandomSequence()`

Ray 1 년 전
부모
커밋
2d1b211920
3개의 변경된 파일69개의 추가작업 그리고 16개의 파일을 삭제
  1. 14 14
      src/external/rprand.h
  2. 4 1
      src/raylib.h
  3. 51 1
      src/rcore.c

+ 14 - 14
src/external/rprand.h

@@ -117,10 +117,10 @@ extern "C" {                // Prevents name mangling of functions
 // Module Functions Declaration
 // Module Functions Declaration
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------
 RPRANDAPI void rprand_set_seed(unsigned long long seed);        // Set rprand_state for Xoshiro128**, seed is 64bit
 RPRANDAPI void rprand_set_seed(unsigned long long seed);        // Set rprand_state for Xoshiro128**, seed is 64bit
-RPRANDAPI unsigned int rprand_get_value(int min, int max);      // Get random value within a range, min and max included
+RPRANDAPI int rprand_get_value(int min, int max);               // Get random value within a range, min and max included
 
 
-RPRANDAPI unsigned int *rprand_load_sequence(unsigned int count, int min, int max); // Load pseudo-random numbers sequence with no duplicates
-RPRANDAPI void rprand_unload_sequence(unsigned int *sequence);  // Unload pseudo-random numbers sequence
+RPRANDAPI int *rprand_load_sequence(unsigned int count, int min, int max); // Load pseudo-random numbers sequence with no duplicates
+RPRANDAPI void rprand_unload_sequence(int *sequence);           // Unload pseudo-random numbers sequence
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }
@@ -136,7 +136,7 @@ RPRANDAPI void rprand_unload_sequence(unsigned int *sequence);  // Unload pseudo
 
 
 #if defined(RPRAND_IMPLEMENTATION)
 #if defined(RPRAND_IMPLEMENTATION)
 
 
-#include <stdlib.h>     // Required for: calloc(), free()
+#include <stdlib.h>     // Required for: calloc(), free(), abs()
 #include <stdint.h>     // Required for data types: uint32_t, uint64_t
 #include <stdint.h>     // Required for data types: uint32_t, uint64_t
 
 
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------
@@ -174,33 +174,33 @@ void rprand_set_seed(unsigned long long seed)
 }
 }
 
 
 // Get random value within a range, min and max included
 // Get random value within a range, min and max included
-unsigned int rprand_get_value(int min, int max)
+int rprand_get_value(int min, int max)
 {
 {
-    unsigned int value = rprand_xoshiro()%(max - min) + min;
+    int value = rprand_xoshiro()%(abs(max - min) + 1) + min;
 
 
     return value;
     return value;
 }
 }
 
 
-// Load pseudo-random numbers sequence with no duplicates
-unsigned int *rprand_load_sequence(unsigned int count, int min, int max)
+// Load pseudo-random numbers sequence with no duplicates, min and max included
+int *rprand_load_sequence(unsigned int count, int min, int max)
 {
 {
-    unsigned int *sequence = NULL;
+    int *sequence = NULL;
     
     
-    if (count > (max - min)) 
+    if (count > (abs(max - min) + 1)) 
     {
     {
         RPRAND_LOG("WARNING: Sequence count required is greater than range provided\n");
         RPRAND_LOG("WARNING: Sequence count required is greater than range provided\n");
         //count = (max - min);
         //count = (max - min);
         return sequence;
         return sequence;
     }
     }
 
 
-    sequence = (unsigned int *)RPRAND_CALLOC(count, sizeof(unsigned int));
+    sequence = (int *)RPRAND_CALLOC(count, sizeof(int));
 
 
-    uint32_t value = 0;
+    int value = 0;
     bool value_is_dup = false;
     bool value_is_dup = false;
 
 
     for (int i = 0; i < count;)
     for (int i = 0; i < count;)
     {
     {
-        value = rprand_xoshiro()%(max - min) + min;
+        value = (rprand_xoshiro()%(abs(max - min) + 1)) + min;
         value_is_dup = false;
         value_is_dup = false;
 
 
         for (int j = 0; j < i; j++)
         for (int j = 0; j < i; j++)
@@ -223,7 +223,7 @@ unsigned int *rprand_load_sequence(unsigned int count, int min, int max)
 }
 }
 
 
 // Unload pseudo-random numbers sequence
 // Unload pseudo-random numbers sequence
-void rprand_unload_sequence(unsigned int *sequence)
+void rprand_unload_sequence(int *sequence)
 {
 {
     RPRAND_FREE(sequence);
     RPRAND_FREE(sequence);
     sequence = NULL;
     sequence = NULL;

+ 4 - 1
src/raylib.h

@@ -1067,10 +1067,13 @@ RLAPI void SwapScreenBuffer(void);                                // Swap back b
 RLAPI void PollInputEvents(void);                                 // Register all input events
 RLAPI void PollInputEvents(void);                                 // Register all input events
 RLAPI void WaitTime(double seconds);                              // Wait for some time (halt program execution)
 RLAPI void WaitTime(double seconds);                              // Wait for some time (halt program execution)
 
 
-// Misc. functions
+// Random values generation functions
 RLAPI void SetRandomSeed(unsigned int seed);                      // Set the seed for the random number generator
 RLAPI void SetRandomSeed(unsigned int seed);                      // Set the seed for the random number generator
 RLAPI int GetRandomValue(int min, int max);                       // Get a random value between min and max (both included)
 RLAPI int GetRandomValue(int min, int max);                       // Get a random value between min and max (both included)
+RLAPI int *LoadRandomSequence(unsigned int count, int min, int max); // Load random values sequence, no values repeated
+RLAPI void UnloadRandomSequence(int *sequence);                   // Unload random values sequence
 
 
+// Misc. functions
 RLAPI void TakeScreenshot(const char *fileName);                  // Takes a screenshot of current screen (filename extension defines format)
 RLAPI void TakeScreenshot(const char *fileName);                  // Takes a screenshot of current screen (filename extension defines format)
 RLAPI void SetConfigFlags(unsigned int flags);                    // Setup init configuration flags (view FLAGS)
 RLAPI void SetConfigFlags(unsigned int flags);                    // Setup init configuration flags (view FLAGS)
 RLAPI void OpenURL(const char *url);                              // Open URL with default system browser (if available)
 RLAPI void OpenURL(const char *url);                              // Open URL with default system browser (if available)

+ 51 - 1
src/rcore.c

@@ -1677,7 +1677,7 @@ void SetRandomSeed(unsigned int seed)
 #endif
 #endif
 }
 }
 
 
-// Get a random value between min and max (both included)
+// Get a random value between min and max included
 int GetRandomValue(int min, int max)
 int GetRandomValue(int min, int max)
 {
 {
     int value = 0;
     int value = 0;
@@ -1695,6 +1695,7 @@ int GetRandomValue(int min, int max)
     // WARNING: Ranges higher than RAND_MAX will return invalid results
     // WARNING: Ranges higher than RAND_MAX will return invalid results
     // More specifically, if (max - min) > INT_MAX there will be an overflow,
     // More specifically, if (max - min) > INT_MAX there will be an overflow,
     // and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold
     // and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold
+    // NOTE: Depending on the library it can be as low as 32767
     if ((unsigned int)(max - min) > (unsigned int)RAND_MAX)
     if ((unsigned int)(max - min) > (unsigned int)RAND_MAX)
     {
     {
         TRACELOG(LOG_WARNING, "Invalid GetRandomValue() arguments, range should not be higher than %i", RAND_MAX);
         TRACELOG(LOG_WARNING, "Invalid GetRandomValue() arguments, range should not be higher than %i", RAND_MAX);
@@ -1705,6 +1706,55 @@ int GetRandomValue(int min, int max)
     return value;
     return value;
 }
 }
 
 
+// Load random values sequence, no values repeated, min and max included
+int *LoadRandomSequence(unsigned int count, int min, int max)
+{
+    int *values = NULL;
+    
+#if defined(SUPPORT_RPRAND_GENERATOR)
+    rprand_load_sequence(count, min, max);
+#else
+    if (count > (abs(max - min) + 1)) return values;
+
+    values = (int *)RL_CALLOC(count, sizeof(int));
+
+    int value = 0;
+    bool dupValue = false;
+
+    for (int i = 0; i < count;)
+    {
+        value = (rand()%(abs(max - min) + 1) + min);
+        dupValue = false;
+
+        for (int j = 0; j < i; j++)
+        {
+            if (values[j] == value)
+            {
+                dupValue = true;
+                break;
+            }
+        }
+
+        if (!dupValue)
+        {
+            values[i] = value;
+            i++;
+        }
+    }
+#endif
+    return values;
+}
+
+// Unload random values sequence
+void UnloadRandomSequence(int *sequence)
+{
+#if defined(SUPPORT_RPRAND_GENERATOR)
+    rprand_unload_sequence(sequence);
+#else
+    RL_FREE(sequence);
+#endif
+}
+
 // Takes a screenshot of current screen (saved a .png)
 // Takes a screenshot of current screen (saved a .png)
 void TakeScreenshot(const char *fileName)
 void TakeScreenshot(const char *fileName)
 {
 {