Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Add GetRandomFloat to API #4156

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/core/core_random_values.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ int main(void)
// SetRandomSeed(0xaabbccff); // Set a custom random seed if desired, by default: "time(NULL)"

int randValue = GetRandomValue(-8, 5); // Get a random integer number between -8 and 5 (both included)
float randFloat = GetRandomFloat(-8.0, 5.0); // Get a random float number between -8 and 5 (both included)

unsigned int framesCounter = 0; // Variable used to count frames

Expand All @@ -45,6 +46,7 @@ int main(void)
if (((framesCounter/120)%2) == 1)
{
randValue = GetRandomValue(-8, 5);
randFloat = GetRandomFloat(-8.0, 5.0);
framesCounter = 0;
}
//----------------------------------------------------------------------------------
Expand All @@ -56,8 +58,10 @@ int main(void)
ClearBackground(RAYWHITE);

DrawText("Every 2 seconds a new random value is generated:", 130, 100, 20, MAROON);
DrawText(TextFormat("%i", randValue), 360, 150, 80, LIGHTGRAY);

DrawText(TextFormat("%i", randValue), 360, 180, 80, LIGHTGRAY);
DrawText("Every 2 seconds a new random float is generated:", 130, 260, 20, MAROON);
DrawText(TextFormat("%.2f", randFloat), 360, 310, 80, LIGHTGRAY);

EndDrawing();
//----------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ RLAPI void WaitTime(double seconds); // Wait for so
// Random values generation functions
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 float GetRandomFloat(float min, float max); // Get a float 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

Expand Down
8 changes: 8 additions & 0 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,14 @@ int GetRandomValue(int min, int max)
return value;
}

// Get a random float between min and max included
float GetRandomFloat(float min, float max)
{
float value = min + ((float)GetRandomValue(0, RAND_MAX) / (float)RAND_MAX) * (max - min);

return value;
}

// Load random values sequence, no values repeated, min and max included
int *LoadRandomSequence(unsigned int count, int min, int max)
{
Expand Down
Loading