Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created June 20, 2015 13:11
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 devendranaga/46911fc66af5e41996af to your computer and use it in GitHub Desktop.
Save devendranaga/46911fc66af5e41996af to your computer and use it in GitHub Desktop.
random number generator in c without using srand or rand
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int rand_gen_int()
{
int ret;
int fd;
int number = 0;
fd = open("/dev/urandom", O_RDWR);
if (fd < 0)
return -1;
ret = read(fd, (void *)&number, sizeof(number));
if (ret < 0) {
close(fd);
return -1;
}
close(fd);
return number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment