1.功能:使用Linux系统提供的机制产生随机数
2.代码
#define RANDPATH "/dev/random"
#define MAGIC_NUMBER 2147483647 // 2^31 - 1
int CreateSystemRandom()
{
int fp, ret;
unsigned int rand;
// Open as read only
fp = open(RANDPATH, O_RDONLY);
if(fp < 0){
fprintf(stderr, "Could not open -%s- for reading.\n",RANDPATH);
return -1;
}
ret = read(fp, &rand, sizeof(rand));
if(ret < 0){
fprintf(stderr, "Could not read from -%s-.\n",RANDPATH);
close(fp);
return ret;
}
fprintf(stdout, "Random value: %u\n", rand);
close(fp);
/* Taking Modulus of MAGIC_NUMBER, so that its converted from unsigned int to int .as fields in rtp_session_config is int for ssrc */
return rand % MAGIC_NUMBER;
}