Created
November 12, 2014 02:46
-
-
Save jinleileiking/7bd492061deb0d7e6b42 to your computer and use it in GitHub Desktop.
random.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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