Is there a function to generate a random int number in C? Or will I have to use a third party library?
As addressed in how to safely generate random numbers in various programming languages, you'll want to do one of the following:
randombytes
API/dev/random
. Not OpenSSL (or other userspace PRNGs).For example:
#include "sodium.h"
int foo()
{
char myString[32];
uint32_t myInt;
if (sodium_init() < 0) {
/* panic! the library couldn't be initialized, it is not safe to use */
return 1;
}
/* myString will be an array of 32 random bytes, not null-terminated */
randombytes_buf(myString, 32);
/* myInt will be a random number between 0 and 9 */
myInt = randombytes_uniform(10);
}
randombytes_uniform()
is cryptographically secure and unbiased.