0

I'm trying to use arc4random_uniform() instead of rand % n in this program of mine, but I'm getting some problems related to my header files.

My code (summarized)

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <time.h>

int main(argc, char *argv[])
{
    //...
    int x = arc4random_uniform(n);
    //...
    return 0;
}

I'm getting the following error whenever I compile with GCC:

warning: implicit declaration of function 'arc4random_uniform' [-Wimplicit-function-declaration]
   70 |         int index = arc4random_uniform(option_amount);
      |                     ^~~~~~~~~~~~~~~~~~
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe

In nearly every source that I find, the family arc4random functions all come from <stdlib.h>. Clearly, that header file is included. I tried reinstalling GCC via MSYS2, but that still didn't work. What could be going on here?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Mailbox
  • 115
  • 1
  • 5
  • `arc4random_uniform` is a POSIX function so obviously it's not available on Windows – phuclv Apr 12 '23 at 22:43
  • @phuclv Excuse me, I wasn't familiar with POSIX until now. Is there any way around that with windows? – Mailbox Apr 13 '23 at 14:24
  • 1
    depends on the language and the level of security required. If cryptographically strong random numbers aren't required then just use the standard `rand()` function. For strong cryptographic numbers in C++ just use the `` header. Otherwise in C use Windows-specific functions like [`rand_s()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/rand-s?view=msvc-170) or [`BCryptGenRandom()`](https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom) – phuclv Apr 13 '23 at 21:03
  • Does this answer your question? [How to generate a random int in C?](https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c) – phuclv Apr 13 '23 at 21:04

0 Answers0