Skip to content

Instantly share code, notes, and snippets.

@rfc-2549
Last active August 9, 2020 14:10
Show Gist options
  • Save rfc-2549/ced4f409ed4628c204356904f673e7c8 to your computer and use it in GitHub Desktop.
Save rfc-2549/ced4f409ed4628c204356904f673e7c8 to your computer and use it in GitHub Desktop.
/* random_number.c:
* Generates a fairly better random number than rand()
* This function grabs x bytes from /dev/urandom
* and converts it to a long long int.
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
unsigned long long int
random_number()
{
int fd = open("/dev/random",O_RDONLY);
unsigned long long int buf;
read(fd,&buf,sizeof(buf)); /* Reads necessary bytes for an unsigned
long long int */
return buf;
}
int
main(void)
{
printf("%llu\n",random_number());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment