Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading from "/dev/random" in c

Tags:

c

random

i am reading high quality 16 bit random numbers of type uint16_t from /dev/random and i am getting numbers as big as: 2936814755. Are these correct

int myFile = open("/dev/random", O_RDONLY);            
unsigned int rand;            
uint16_t randomNum = read(myFile, &rand, sizeof(rand)) ;
printf(" %u ", rand);
close(myFile);
like image 480
pnizzle Avatar asked Mar 11 '26 17:03

pnizzle


1 Answers

unsigned int probably isn't 16 bit on your pc architecture. If you want to be sure use uint16_t instead.

uint16_t rand;            
int ret = read(myFile, &rand, sizeof(rand)) ;

I think you were confusing the return value of read (ret that should be int and is the numbers of bytes read) and the generated random number (rand that should be uint16_t and is the random number generated).

like image 170
Heisenbug Avatar answered Mar 13 '26 08:03

Heisenbug