Random number generator not working

I use this function to create random numbers between 100000000 and 999999999

int irand(int start, int stop) {
  double range = stop - start + 1;
  return start + (int)(range * rand()/(RAND_MAX+1.0));
}

When I use it like this, it works correctly

while(1) {
    DWORD dw = irand(100000000, 999999999);
    printf("dynamic key: %d\n", dw);
    Sleep(100);
}

But when I use this function without while (1), I always get the same number. What do I need to fix?

+3
source share
2 answers

, , 1 rand. srand (time(NULL)); , irand , . : rand()?

+3

Source: https://habr.com/ru/post/1766538/


All Articles