Skip to content

Instantly share code, notes, and snippets.

@jinleileiking
Created November 12, 2014 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinleileiking/7bd492061deb0d7e6b42 to your computer and use it in GitHub Desktop.
Save jinleileiking/7bd492061deb0d7e6b42 to your computer and use it in GitHub Desktop.
random.c
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
ssize_t get_random()
{
int randomData = open("/dev/random", O_RDONLY);
int myRandomInteger;
size_t randomDataLen = 0;
while (randomDataLen < sizeof myRandomInteger)
{
ssize_t result = read(randomData, ((char*)&myRandomInteger) + randomDataLen, (sizeof myRandomInteger) - randomDataLen);
if (result < 0)
{
// error, unable to read /dev/random
}
randomDataLen += result;
}
close(randomData);
return result;
}
main()
{
printf("%d\n", get_random());
printf("%d\n", get_random());
printf("%d\n", get_random());
printf("%d\n", get_random());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment