开发者

reading from "/dev/random" in c

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);


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).


Change 'unsigned int rand' to 'unsigned short rand' in your code and you will be fine !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜