core_random_values.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Generate random values
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 1.1, last time updated with raylib 1.1
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - generate random values");
  26. // SetRandomSeed(0xaabbccff); // Set a custom random seed if desired, by default: "time(NULL)"
  27. int randValue = GetRandomValue(-8, 5); // Get a random integer number between -8 and 5 (both included)
  28. unsigned int framesCounter = 0; // Variable used to count frames
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //--------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. framesCounter++;
  37. // Every two seconds (120 frames) a new random value is generated
  38. if (((framesCounter/120)%2) == 1)
  39. {
  40. randValue = GetRandomValue(-8, 5);
  41. framesCounter = 0;
  42. }
  43. //----------------------------------------------------------------------------------
  44. // Draw
  45. //----------------------------------------------------------------------------------
  46. BeginDrawing();
  47. ClearBackground(RAYWHITE);
  48. DrawText("Every 2 seconds a new random value is generated:", 130, 100, 20, MAROON);
  49. DrawText(TextFormat("%i", randValue), 360, 180, 80, LIGHTGRAY);
  50. EndDrawing();
  51. //----------------------------------------------------------------------------------
  52. }
  53. // De-Initialization
  54. //--------------------------------------------------------------------------------------
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }