raylib and srand: fps speed

59 Views Asked by At

my program shows my string on the screen with different speed.

It updates my gui very fast when I'm setting the random seed (with srand(...)) in the main function and the gui changes very slow with a call to srand(...) in my get_number() function.

What is the reason for that strange behaviour?

PS: Yes, I probably shouldn't call srand() in a function, which is called 60 times per second and to set the random seed once at the beginning of my program would be enough.

Here is an short example for my problem:

#include <raylib.h>
#include <stdlib.h>
#include <time.h>

#define MAX_STR_LEN 2

char str[MAX_STR_LEN] = "";

char *get_number(void) {
  // runs slow with this line
  // srand(time(NULL));
  
  str[0] = '0' + rand() % 10;
  
  return str;
}

int main(void) {
  // runs fast with this line
  // srand(time(NULL));

  const int w = 800;
  const int h = 400;

  InitWindow(w, h, "");
  SetTargetFPS(60);

  while (!WindowShouldClose()) {
    BeginDrawing();
    ClearBackground(WHITE);
    DrawText(get_number(), w / 2, h / 2, 50, BLACK);
    EndDrawing();
  }

  CloseWindow();
 
  return EXIT_SUCCESS;
}

Actually I was expecting almost the same execution speed.

EDIT:

The explanation for that "strange" behaviour is the following: I'm using time(NULL) as a seed for the srand-function.

That means, that I have the same seed number for a whole second.

Although it's called 60 times per second - I always geht the same number. The program is not slower - it just showing the same number for a second.

Thanks to user pmacfarlane!

0

There are 0 best solutions below