diff options
| author | Ray <[email protected]> | 2023-11-03 20:21:43 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2023-11-03 20:21:43 +0100 |
| commit | 2d1b2119202d08245ca4d313b846a6d0a8b2139b (patch) | |
| tree | 644c1c2c055704fdbfb81735ea5976661408d8d3 /src/rcore.c | |
| parent | 5da0074fed50f3a63d3c47aa0f2e1fbdc9051059 (diff) | |
| download | raylib-2d1b2119202d08245ca4d313b846a6d0a8b2139b.tar.gz raylib-2d1b2119202d08245ca4d313b846a6d0a8b2139b.zip | |
ADDED: `LoadRandomSequence()`/`UnloadRandomSequence()`
Diffstat (limited to 'src/rcore.c')
| -rw-r--r-- | src/rcore.c | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/rcore.c b/src/rcore.c index ac5dea84..b09dffe6 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -1677,7 +1677,7 @@ void SetRandomSeed(unsigned int seed) #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 value = 0; @@ -1695,6 +1695,7 @@ int GetRandomValue(int min, int max) // WARNING: Ranges higher than RAND_MAX will return invalid results // 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 + // NOTE: Depending on the library it can be as low as 32767 if ((unsigned int)(max - min) > (unsigned int)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; } +// 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) void TakeScreenshot(const char *fileName) { |
